I am trying to bind List<bool>
from MainWindow to my UserControl. MainWindow obtained list from other UserControl. I wanted to bind it to its child control and do some actions when element of the List was changed, so I wrote this piece of code:
MainWindow class:
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 class:
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");
}
And when i break at some point in code (after clicking button) ButtonsBacklitList
is always null. What should i change or add to do this correctly?