如何绑定附加属性

问题描述 投票:0回答:2

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

 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"));

}

我将此附加属性放置在我的自定义datagrid用户控件中,该控件在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>

问题是如何在Custom Datagrid User Control中绑定该附加属性值?示例:

<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? ---->}"
              >
c# wpf bind attached-properties
2个回答
1
投票

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

此外,声明类不必从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类中将该属性声明为常规依赖项属性。

您正在定义一个简单的DepencyProperty。您必须使用DependencyProperty.RegisterAttached方法将DependencyProperty注册为附加属性。同样,所有者类型必须设置为声明类的类型(typeof(AQDatagridDependencyProperties)),并且not

附加类型(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(UIElement element, string value)
  {
    element.SetValue(CustomDataSourceProperty, value);
  }

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

用法

<Wpf:AQDataGrid Wpf:AQDatagridDependencyProperties.CustomDataSource="Something" />

AQDataGrid控件内部:

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

1
投票

您正在定义一个简单的DepencyProperty。您必须使用DependencyProperty.RegisterAttached方法将DependencyProperty注册为附加属性。同样,所有者类型必须设置为声明类的类型(typeof(AQDatagridDependencyProperties)),并且not

© www.soinside.com 2019 - 2024. All rights reserved.