uwp:数据绑定编程问题
uwp: data binding programmatically issue
我正在使用 uwp 进行开发,但遇到了数据绑定问题。我有一个 listView,其中填充了一个名为 PlaylistLeftOption class 的自定义面板元素。此 class 继承 Panel class 属性,继承 FrameworkElement class 属性及其方法,因此我有一个可用的 SetBinding 方法。
现在我试图绑定高度值(它等于其他元素)所以我在其他外部单例 class 中创建了一个名为 PerformanceItemHeight 的静态属性。
因为我需要动态地填充列表视图,所以我试图将值绑定到构造函数中,但它不起作用。
这是构造函数中的代码:
public PlaylistLeftOption()
{
mainGrid.Background = new SolidColorBrush(Colors.Red);
mainGrid.BorderBrush = new SolidColorBrush(Colors.Black);
mainGrid.BorderThickness = new Thickness(0.5,0.25,0.5,0.25);
WidthVal = 200;
HeightVal = 50;
var myBinding = new Binding();
myBinding.Source = PerformanceLayout.Instance.PerformanceItemHeight;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.Mode = BindingMode.TwoWay;
SetBinding(HeightValProperty, myBinding);
Children.Add(mainGrid);
}
这是 属性:
public static readonly DependencyProperty HeightValProperty = DependencyProperty.Register(
"HeightVal",
typeof(double),
typeof(PlaylistLeftOption),
new PropertyMetadata(50)
);
public double HeightVal
{
get => (double)GetValue(HeightValProperty);
set
{
SetValue(HeightValProperty, value);
Height = HeightVal;
mainGrid.Height = HeightVal;
globalSize.Height = HeightVal;
}
}
这是 PerformanceItemHeight 的代码:
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
// Raise the PropertyChanged event, passing the name of the property whose value has changed.
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private double _performanceItemHeight = 50;
public double PerformanceItemHeight {
get => _performanceItemHeight;
set {
_performanceItemHeight = value;
this.OnPropertyChanged();
}
}
为什么 via xaml 有效?
我尝试通过 xaml 在列表视图中添加 PlaylistLeftOption 项,没问题!
谢谢
通过测试,HeightVal
的绑定在 XAML 中有效,而 HeightVal
的绑定在代码隐藏中无效。您可以在文档 Custom dependency properties 的 Implementing the wrapper 部分中看到原因,其中指出您的包装器实现应仅执行 GetValue 和 SetValue 操作。否则,通过 XAML 设置 属性 与通过代码设置 属性 时会出现不同的行为。
您可以添加一个property-changed callback method来主动通知HeightVal
的变化。
例如:
public static readonly DependencyProperty HeightValProperty = DependencyProperty.Register(
"HeightVal",
typeof(double),
typeof(PlaylistLeftOption),
new PropertyMetadata(100, new PropertyChangedCallback(OnHeightValChanged))
);
private static void OnHeightValChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PlaylistLeftOption playlistLeftOption = d as PlaylistLeftOption;
if(playlistLeftOption != null)
{
var height = (Double)e.NewValue;
playlistLeftOption.HeightVal = height;
}
}
并像这样更改绑定代码:
var myBinding = new Binding();
myBinding.Source = PerformanceLayout.Instance;
myBinding.Path = new PropertyPath("PerformanceItemHeight");
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.Mode = BindingMode.TwoWay;
SetBinding(HeightValProperty, myBinding);
我正在使用 uwp 进行开发,但遇到了数据绑定问题。我有一个 listView,其中填充了一个名为 PlaylistLeftOption class 的自定义面板元素。此 class 继承 Panel class 属性,继承 FrameworkElement class 属性及其方法,因此我有一个可用的 SetBinding 方法。 现在我试图绑定高度值(它等于其他元素)所以我在其他外部单例 class 中创建了一个名为 PerformanceItemHeight 的静态属性。 因为我需要动态地填充列表视图,所以我试图将值绑定到构造函数中,但它不起作用。 这是构造函数中的代码:
public PlaylistLeftOption()
{
mainGrid.Background = new SolidColorBrush(Colors.Red);
mainGrid.BorderBrush = new SolidColorBrush(Colors.Black);
mainGrid.BorderThickness = new Thickness(0.5,0.25,0.5,0.25);
WidthVal = 200;
HeightVal = 50;
var myBinding = new Binding();
myBinding.Source = PerformanceLayout.Instance.PerformanceItemHeight;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.Mode = BindingMode.TwoWay;
SetBinding(HeightValProperty, myBinding);
Children.Add(mainGrid);
}
这是 属性:
public static readonly DependencyProperty HeightValProperty = DependencyProperty.Register(
"HeightVal",
typeof(double),
typeof(PlaylistLeftOption),
new PropertyMetadata(50)
);
public double HeightVal
{
get => (double)GetValue(HeightValProperty);
set
{
SetValue(HeightValProperty, value);
Height = HeightVal;
mainGrid.Height = HeightVal;
globalSize.Height = HeightVal;
}
}
这是 PerformanceItemHeight 的代码:
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
// Raise the PropertyChanged event, passing the name of the property whose value has changed.
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private double _performanceItemHeight = 50;
public double PerformanceItemHeight {
get => _performanceItemHeight;
set {
_performanceItemHeight = value;
this.OnPropertyChanged();
}
}
为什么 via xaml 有效? 我尝试通过 xaml 在列表视图中添加 PlaylistLeftOption 项,没问题! 谢谢
通过测试,HeightVal
的绑定在 XAML 中有效,而 HeightVal
的绑定在代码隐藏中无效。您可以在文档 Custom dependency properties 的 Implementing the wrapper 部分中看到原因,其中指出您的包装器实现应仅执行 GetValue 和 SetValue 操作。否则,通过 XAML 设置 属性 与通过代码设置 属性 时会出现不同的行为。
您可以添加一个property-changed callback method来主动通知HeightVal
的变化。
例如:
public static readonly DependencyProperty HeightValProperty = DependencyProperty.Register(
"HeightVal",
typeof(double),
typeof(PlaylistLeftOption),
new PropertyMetadata(100, new PropertyChangedCallback(OnHeightValChanged))
);
private static void OnHeightValChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PlaylistLeftOption playlistLeftOption = d as PlaylistLeftOption;
if(playlistLeftOption != null)
{
var height = (Double)e.NewValue;
playlistLeftOption.HeightVal = height;
}
}
并像这样更改绑定代码:
var myBinding = new Binding();
myBinding.Source = PerformanceLayout.Instance;
myBinding.Path = new PropertyPath("PerformanceItemHeight");
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.Mode = BindingMode.TwoWay;
SetBinding(HeightValProperty, myBinding);