【问题标题】:WPF: Binding List from parent to childWPF:从父级到子级的绑定列表
【发布时间】:2016-05-15 22:40:18
【问题描述】:

我正在尝试将 List<bool> 从 MainWindow 绑定到我的 UserControl。 MainWindow 从其他 UserControl 获取列表。我想将它绑定到它的子控件并在 List 的元素发生更改时执行一些操作,所以我编写了这段代码:

MainWindow 类:

public static readonly DependencyProperty mRowsInfoProperty =
    DependencyProperty.Register("mRowsInfo", typeof(List<bool>), typeof(MainWindow),
        new FrameworkPropertyMetadata(new List<bool>()));

public List<bool> mRowsInfo
{
    get { return (List<bool>)GetValue(mRowsInfoProperty); }
    set { SetValue(mRowsInfoProperty, value); }
}
public List<bool> RowsInfo
{
    get { return mRowsInfo; }
    set
    {
        mRowsInfo = value;
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("RowsInfo"));
    }
}

MainWindow xaml:

<local:CustomGrid isRowValid="{Binding RowsInfo, Mode=TwoWay}">
<local:ControlButtons ButtonsBacklitList="{Binding mRowsInfo, ElementName=myWindow}"/>

用户控制类:

 public static readonly DependencyProperty ButtonsBacklitListProperty =
            DependencyProperty.Register("ButtonsBacklitList", typeof(ObservableCollection<bool>), typeof(ControlButtons),
                new FrameworkPropertyMetadata(new ObservableCollection<bool>(), onBBLCallBack));

        public ObservableCollection<bool> ButtonsBacklitList
        {

            get { return (ObservableCollection<bool>)GetValue(ButtonsBacklitListProperty); }
            set { SetValue(ButtonsBacklitListProperty, value); }
        }
        private static void onBBLCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ControlButtons h = sender as ControlButtons;
            if (h != null)
            {
                h.onBBLChanged();
            }
        }
        protected virtual void onBBLChanged()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("ButtonsBacklitList"));
            Console.WriteLine("UPDATED");
        }

当我在代码中的某个点中断时(单击按钮后)ButtonsBacklitList 始终为空。我应该更改或添加什么才能正确执行此操作?

【问题讨论】:

  • 首先,为什么INotifyPropertyChangedDependencyProperty 一起使用?将 List&lt;bool&gt; 实现为 DependencyProperty 将允许在重新实例化列表(或重新分配给不同引用地址的另一个列表)时触发绑定。

标签: c# wpf list xaml binding


【解决方案1】:

MainWindow 类:

public static readonly DependencyProperty RowsInfoProperty =
    DependencyProperty.Register("RowsInfo", typeof(ObservableCollection<bool>), typeof(MainWindow),
        new FrameworkPropertyMetadata(new ObservableCollection<bool>()));

public ObservableCollection<bool> RowsInfo
{
    get { return (ObservableCollection<bool>)GetValue(RowsInfoProperty); }
    set { SetValue(RowsInfoProperty, value); }
}

主窗口 xaml:

<local:CustomGrid isRowValid="{Binding RowsInfo, Mode=TwoWay}">
<local:ControlButtons ButtonsBacklitList="{Binding RowsInfo, ElementName=myWindow}"/>

用户控件类:

public static readonly DependencyProperty ButtonsBacklitListProperty =
    DependencyProperty.Register("ButtonsBacklitList", typeof(ObservableCollection<bool>), typeof(ControlButtons),
        new FrameworkPropertyMetadata(new ObservableCollection<bool>(), onBBLCallBack));

public ObservableCollection<bool> ButtonsBacklitList
{
    get { return (ObservableCollection<bool>)GetValue(ButtonsBacklitListProperty); }
    set { SetValue(ButtonsBacklitListProperty, value); }
}

// Not sure why you want to capture changes, I hope it's not for binding
// But I'm copying it anyway
private static void onBBLCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    ControlButtons h = sender as ControlButtons;
    if (h != null)
    {
        h.onBBLChanged();
    }
}

// This looks super suspicious because all you did is to trigger INotifyPropertyChanged's handler!
// I hope somewhere in your code you actually need to manually handle this change event...
protected virtual void onBBLChanged()
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("ButtonsBacklitList"));
    Console.WriteLine("UPDATED");
}

首先,我感觉您对绑定概念不太熟悉。实现为List&lt;bool&gt; 意味着绑定只会对整个属性引用的变化做出反应(例如this.RowInfo = new List&lt;bool&gt;)。

其次,不知道为什么要将DependencyPropertyINotifyPropertyChanged 叠加。对于要成为绑定 source 的属性(对于接收更改通知的绑定),它只需要是这两者之一。但是,要使属性成为绑定目标,它必须是DependencyProperty。比如&lt;local:CustomGrid isRowValid="{Binding RowsInfo, Mode=TwoWay}"&gt;中,isRowValid是绑定目标,这必须是DependencyProperty,而RowsInfo是绑定源,可以是两者之一。

希望能帮到你。

【讨论】:

  • 非常感谢您的回答,非常有帮助。说真的,这是我第一个使用 wpf 的项目,所以我需要练习绑定其他机制。我需要捕获集合项目的更改,而不是集合本身。这是我的问题的一部分。我读了this page,但我不确定哪个答案能解决我的问题
  • 初期是最痛苦的。我也很新,大约 2 个月前开始,之前没有 C#、WPF 或 MVVM 方面的经验。那时我也经常处于迷茫状态。在阅读了大量文章和 SO 问题/答案,然后尝试了之后,我想我现在对绑定(MVVM 方式)非常熟悉。
猜你喜欢
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
相关资源
最近更新 更多