如何将附加属性绑定到子元素

时间:2019-01-07 14:15:36

标签: c# wpf data-binding attached-properties

我想为数据网格创建一个模板样式,它将具有一个默认的contextmenu菜单,并带有一个来自附加属性的ItemsSource。 问题是我的绑定不起作用,因为找到的附加属性值始终为0。

我的上下文菜单项在集合中

public class PaginatedCollection<T> : ObservableCollection<T>
{
   public ObservableCollection<ContextMenuItem> CollectionContextMenu { get; set; }
}

此集合是自定义可观察集合的子对象,该集合可用作数据网格的ItemSource

它的构造函数总是为CollectionContextMenu创建两个项目:
-ExcelExport
-CopyValue

我已经创建了一个附加属性:

public class DataGridContextMenu : DependencyObject
{
    public static ObservableCollection<ContextMenuItem> GetContextMenuItems(DependencyObject contextMenuItemsProperty)
    {
        return (ObservableCollection<ContextMenuItem>)contextMenuItemsProperty.GetValue(ContextMenuItemsProperty);
    }

    public static void SetContextMenuItems(DependencyObject contextMenuItemsProperty, ObservableCollection<ContextMenuItem> value)
    {
        contextMenuItemsProperty.SetValue(ContextMenuItemsProperty, value);
    }

    // Using a DependencyProperty as the backing store for ContextMenuItems.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ContextMenuItemsProperty =
        DependencyProperty.Register("ContextMenuItems", typeof(ObservableCollection<ContextMenuItem>), typeof(DataGridContextMenu), new UIPropertyMetadata(null));

}

我也已经定义了那些样式:

<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
  <Setter Property="MenuItem.Header" Value="{Binding Header}"/>
  <Setter Property="MenuItem.ToolTip" Value="{Binding ToolTip}"/>
  <Setter Property="MenuItem.Icon" Value="{Binding Icone}"/>
  <Setter Property="MenuItem.Command" Value="{Binding Command}"/>
  <Setter Property="MenuItem.CommandParameter" Value="{Binding CommandParameter}"/>
</Style>
<Style x:Key="GridWithContext" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource MaterialDesignDataGrid}">
  <Setter Property="ContextMenu">
    <Setter.Value>
      <ContextMenu ItemsSource="{Binding Path=(ap:DataGridContextMenu.ContextMenuItems), UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}" ItemContainerStyle="{StaticResource ContextMenuItemStyle}" />
    </Setter.Value>
  </Setter>
</Style>

然后在我的xaml文件中以这种方式使用它:

<DataGrid x:Name="dg_affaires" ItemsSource="{Binding Path=Collection, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridWithContext}" ap:DataGridContextMenu.ContextMenuItems="{Binding Collection.CollectionContextMenu, UpdateSourceTrigger=PropertyChanged}"/>

与此有关,不显示上下文菜单。 我知道问题出在我的数据网格样式绑定上。
我使用snoop发现了此错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=(0); DataItem=null; target element is 'ContextMenu' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

Path =(0)告诉我附加属性值错误。

我尝试将集合直接放在样式上,效果很好,但是如果不使用附加属性,系统就会失去兴趣。

有人知道我在做什么错吗?

0 个答案:

没有答案