J'ai un problème de liaison dans XAML/WPF. J'ai créé la classe Action qui étend FrameworkElement. Chaque action a une liste d'ActionItem. Le problème est que les propriétés Data/DataContext de ActionItem ne sont pas définies, elles sont donc toujours NULL.Problème de liaison WPF
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");
}
}
Toutes les idées?
Qu'est-ce que le DataContext du conteneur? c'est-à-dire le premier {Binding} dans votre xaml – kiwipom