如何绑定附加属性

How to bind AttachedProperty

我创建了附件 属性,用于学习目的,但无法获得成功的结果。

 public class AQDatagridDependencyProperties: DependencyObject
{
    public static void SetCustomDataSource(AQDataGrid element, string value)
    {
        element.SetValue(CustomDataSourceProperty, value);
    }

    public static string GetCustomDataSource(AQDataGrid element)
    {
        return (string)element.GetValue(CustomDataSourceProperty);
    }

    // Using a DependencyProperty as the backing store for DataSource.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CustomDataSourceProperty =
        DependencyProperty.Register("CustomDataSource", typeof(string), typeof(AQDataGrid), new PropertyMetadata("obuolys"));

}

我已将此附件 属性 放在我在 UserView 页面中实现的自定义数据网格用户控件中。

<Page x:Class="PDB.UsersView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:PDB"
  xmlns:PDB ="clr-namespace:PDBapi;assembly=PDBapi"
  xmlns:Wpf ="clr-namespace:AQWpf;assembly=AQWpf"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="800"
  Title="Users"
  Name="Users"
  VisualBitmapScalingMode="LowQuality"

  >

<Page.DataContext>
    <PDB:UsersViewModel x:Name="vm"/>
</Page.DataContext>

<Grid VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling">
    <Wpf:AQDataGrid DataContext="{Binding AQDatagridViewModel}" Wpf:AQDatagridDependencyProperties.CustomDataSource="Something" />
</Grid>

问题是如何在自定义数据网格用户控件中绑定附加的 属性 值?示例:

<UserControl x:Class="AQWpf.AQDataGrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:AQWpf"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         mc:Ignorable="d"              
         Name="AQCustomDataGrid"

         >
<!--Custom Data grid Implementation-->

    <DataGrid x:Name="InstructionsDataGrid" 
              Grid.Row="1"
              DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:AQDataGrid}, Path=DataContext}"
              Style="{StaticResource OptimizedAGDatagrid}"
              ItemsSource="{Binding Data}"
              CurrentItem="{Binding SelectedObject, Mode=TwoWay}"
              CurrentColumn="{Binding CurrentColumn, Mode=TwoWay}"
              CurrentCell="{Binding CurrentCells, Mode=TwoWay}"  
              Tag="<----How to bind here? ---->}"
              >

您正在定义一个简单的 DepencyProperty。您必须使用 DependencyProperty.RegisterAttached 方法将 DependencyProperty 注册为附加的 属性。
此外,所有者类型必须设置为声明 class' 类型 (typeof(AQDatagridDependencyProperties)) 并且 而不是 附加类型 (typeof(AQDataGrid)):

AQDatagridDependencyProperties.cs

public class AQDatagridDependencyProperties : DependencyObject
{
  public static readonly DependencyProperty CustomDataSourceProperty = DependencyProperty.RegisterAttached(
    "CustomDataSource",
    typeof(string),
    typeof(AQDatagridDependencyProperties),
    new FrameworkPropertyMetadata("obuolys", AQDatagridDependencyProperties.DebugPropertyChanged));

  private static void DebugPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    var oldValue = e.OldValue; // Set breakpoints here
    var newValue = e.NewValue; // in order to track property changes 
  }

  public static void SetCustomDataSource(DependencyObject attachingElement, string value)
  {
    attachingElement.SetValue(CustomDataSourceProperty, value);
  }

  public static string GetCustomDataSource(DependencyObject attachingElement)
  {
    return (string) attachingElement.GetValue(CustomDataSourceProperty);
  }
}

用法

<AQDataGrid AQDatagridDependencyProperties.CustomDataSource="Something" />

AQDataGrid 控件内:

<DataGrid x:Name="InstructionsDataGrid" 
          Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=AQDataGrid}, Path=(AQDatagridDependencyProperties.CustomDataSource)}" />

您所附的 属性 声明不正确。您必须调用 RegisterAttached 而不是 Register,并且传递给该方法的第三个参数必须是声明 属性.

的 class 的类型

除此之外,声明 class 不需要从 DependencyObject 派生,甚至可以声明为静态的:

public static class AQDatagridDependencyProperties
{
    public static readonly DependencyProperty CustomDataSourceProperty =
         DependencyProperty.RegisterAttached( // here
             "CustomDataSource",
             typeof(string),
             typeof(AQDatagridDependencyProperties), // and here
             new PropertyMetadata("obuolys"));

    public static string GetCustomDataSource(AQDataGrid element)
    {
        return (string)element.GetValue(CustomDataSourceProperty);
    }

    public static void SetCustomDataSource(AQDataGrid element, string value)
    {
        element.SetValue(CustomDataSourceProperty, value);
    }
} 

您可以将 属性 设置为

 <local:AQDataGrid local:AQDatagridDependencyProperties.CustomDataSource="something" >

并通过像

这样的表达式绑定到它
Tag="{Binding Path=(local:AQDatagridDependencyProperties.CustomDataSource),
              RelativeSource={RelativeSource AncestorType=UserControl}}"

请注意,您通常会将 属性 声明为 AQDataGrid class 中的常规依赖项 属性。