WPF binding problem
Posted
by Lolo
on Stack Overflow
See other posts from Stack Overflow
or by Lolo
Published on 2010-06-18T10:27:08Z
Indexed on
2010/06/18
10:43 UTC
Read the original article
Hit count: 172
I've got problem with binding in XAML/WPF. I created Action class witch 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?
© Stack Overflow or respective owner