XAML uwp:数据绑定以编程方式发出

kh212irz  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(122)

我正在开发uwp,我有一个数据绑定的问题。我有一个listView,我用一个名为PlaylistLeftOption类的自定义面板元素填充。这个类继承了Panel类的属性,而Panel类的属性又继承了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中添加PlaylistLeftOption项,没问题!谢谢

fcwjkofz

fcwjkofz1#

通过测试,HeightVal的绑定在XAML中工作,而HeightVal的绑定在代码隐藏中不工作。您可以在自定义依赖属性文档的实现 Package 器一节中看到原因,其中说明您的 Package 器实现应仅执行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;
    }
}

然后像这样修改binging代码:

var myBinding = new Binding();
myBinding.Source = PerformanceLayout.Instance;
myBinding.Path = new PropertyPath("PerformanceItemHeight");
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.Mode = BindingMode.TwoWay;
SetBinding(HeightValProperty, myBinding);

相关问题