簡體   English   中英

WPF:綁定列表從父級到子級

[英]WPF: Binding List from parent to child

我試圖將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}"/>

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始終為null。 我應該更改或添加哪些內容才能正確執行此操作?

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> )。

其次,不確定為什么要使用INotifyPropertyChanged堆疊DependencyProperty 為了使屬性成為綁定 (對於接收更改通知的綁定),只需將其作為這兩個。 要使屬性成為綁定目標 ,它必須是DependencyProperty 例如,在<local:CustomGrid isRowValid="{Binding RowsInfo, Mode=TwoWay}">isRowValid是綁定目標,它必須是DependencyProperty ,而RowsInfo是綁定源,並且可以是兩者之一。

希望我能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM