Xaml uwp:数据绑定编程问题
我正在使用uwp进行开发,在数据绑定方面遇到了一个问题。我有一个listView,其中填充了一个名为PlayLeftOption类的自定义面板元素。该类继承继承FrameworkElement类属性及其方法的面板类属性,因此我有一个可用的SetBinding方法。 现在我试图绑定height值(它等于其他元素),所以我在其他extern单例类中创建了一个静态属性,称为PerformanceItemHeight。 因为我需要动态地填充listview,所以我试图在构造函数中绑定该值,但它不起作用。 这是构造函数中的代码:Xaml uwp:数据绑定编程问题,xaml,data-binding,uwp,binding,programmatically,Xaml,Data Binding,Uwp,Binding,Programmatically,我正在使用uwp进行开发,在数据绑定方面遇到了一个问题。我有一个listView,其中填充了一个名为PlayLeftOption类的自定义面板元素。该类继承继承FrameworkElement类属性及其方法的面板类属性,因此我有一个可用的SetBinding方法。 现在我试图绑定height值(它等于其他元素),所以我在其他extern单例类中创建了一个静态属性,称为PerformanceItemHeight。 因为我需要动态地填充listview,所以我试图在构造函数中绑定该值,但它不起作用。
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();
}
}
为什么通过xaml它可以工作?
我试着通过xaml在listview中添加PlayLeftOption项,没问题!
谢谢通过测试,
HeightVal
的绑定在XAML中工作,HeightVal
的绑定在代码隐藏中不工作。您可以在文档的实现包装器部分中看到原因,该部分指出包装器实现应该只执行GetValue和SetValue操作。否则,当通过XAML设置属性与通过代码设置属性时,您将获得不同的行为
您可以添加一个通知来主动通知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;
}
}
并更改Bing代码,如下所示:
var myBinding = new Binding();
myBinding.Source = PerformanceLayout.Instance;
myBinding.Path = new PropertyPath("PerformanceItemHeight");
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.Mode = BindingMode.TwoWay;
SetBinding(HeightValProperty, myBinding);
杨古:非常感谢!它工作正常!如果答案解决了您的问题,请将其视为已接受