I've got a problem with binding in XAML/WPF. I created Action class which extends FrameworkElement. Each Action has list of ActionItem. The problem is that the Data/DataContext properties of ActionItem are not set, so they are always null.
XAML:
<my:Action DataContext="{Binding}">
<my:Action.Items>
<my:ActionItem DataContext="{Binding}" Data="{Binding}" />
</my:Action.Items>
</my:Action>
C#:
public class Action : FrameworkElement
{
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(IList), typeof(Action),
new PropertyMetadata(null, null), null);
public Action()
{
this.Items = new ArrayList();
this.DataContextChanged += (s, e) => MessageBox.Show("Action.DataContext");
}
public IList Items
{
get { return (IList)this.GetValue(ItemsProperty); }
set { this.SetValue(ItemsProperty, value); }
}
}
public class ActionItem : FrameworkElement
{
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(ActionItem),
new PropertyMetadata(
null, null, (d, v) =>
{
if (v != null)
MessageBox.Show("ActionItem.Data is not null");
return v;
}
), null
);
public object Data
{
get { return this.GetValue(DataProperty); }
set { this.SetValue(DataProperty, value); }
}
public ActionItem()
{
this.DataContextChanged += (s, e) => MessageBox.Show("ActionItem.DataContext");
}
}
Any ideas?