WPF:从父级到子级的绑定列表

时间:2016-05-15 22:40:18

标签: c# wpf list xaml binding

我正在尝试将MainWindow中的List<bool>绑定到我的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}"/>

UserControl类:

 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始终为空。我应该更改或添加什么来正确执行此操作?

1 个答案:

答案 0 :(得分: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); }
}

MainWindow xaml:

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

UserControl类:

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<bool>意味着绑定只会对整个属性引用中的更改做出反应(例如this.RowInfo = new List<bool>)。

其次,不确定为什么要将DependencyPropertyINotifyPropertyChanged堆叠在一起。对于属性是绑定(对于接收更改通知的绑定),它只需要是这两者中的任何一个。要使属性成为绑定目标,它必须是DependencyProperty。例如,在<local:CustomGrid isRowValid="{Binding RowsInfo, Mode=TwoWay}">中,isRowValid是绑定目标,必须是DependencyProperty,而RowsInfo是绑定源,它可以是两者之一。

我希望我能成功帮助你。