我创建了附加属性,用于学习目的,但无法获得成功的结果。
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"));
}
我已将此附加属性放置在在用户视图页中实现的自定义数据网格用户控件中。
<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? ---->}"
>
附加的属性声明不正确。您必须调用RegisterAttached
而不是Register
,传递给该方法的第三个参数必须由声明属性的类的类型。
除此之外,声明类不需要从DependencyObject
派生,甚至可以声明为 static:
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)
(而不是附加类型(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)}" />