Silverlight DataGrid列附加属性
我正在尝试在 Silverlight 3.0 中为 DataGridColumn
创建一个 AttachedProperty
,但遇到了一些问题。
这是 AttachedProperty:
public class DataGridColumnHelper
{
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.RegisterAttached("Header", typeof(string), typeof(DataGridColumnHelper),
new PropertyMetadata(OnHeaderPropertyChanged));
private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string header = GetHeader(d);
var dataGridColumn = d as DataGridColumn;
if (dataGridColumn == null)
{
return;
}
dataGridColumn.Header = GetHeader(dataGridColumn);
}
public static string GetHeader(DependencyObject obj)
{
return (string)obj.GetValue(HeaderProperty);
}
public static void SetHeader(DependencyObject obj, string value)
{
obj.SetValue(HeaderProperty, value);
}
}
如您所见,它非常简单,我正在尝试克服 DataGridColumn 类中的 Header 属性无法绑定的限制。
此 XAML 按预期工作...
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="User Name"/>
但是此 XAML 引发错误...(具体来说:{System.Windows.Markup.XamlParseException:AG_E_PARSER_PROPERTY_NOT_FOUND [行:224 位置:112] 在System.Windows.Application.LoadComponent(对象组件,Uri资源定位器) ....})
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding Resources.UserNameListViewHeading, Source={StaticResource Labels}}"/>
只是为了进行实验,我将此属性(使用上面的绑定语法)附加到 DataGrid
并检查了 OnHeaderPropertyChanged 中的
方法并且值是正确的(并且未引发异常)DataGridColumnHelper.Header
属性
据我所知,AttachedProperty 附加到的对象必须是 DependencyProperty
。通过 Reflector 观察,DataGridColumn
(DataGridTextColumn
从中派生)派生自 DependencyProperty
。
有人可以解释一下吗?我正在尝试本地化我们的应用程序,但在使用 DataGrid 时遇到了问题。我确信我可以在代码隐藏中做到这一点,但我试图避免这种情况。
I am attempting to create an AttachedProperty
for a DataGridColumn
within Silverlight 3.0 and I am having some issues.
Here is the AttachedProperty:
public class DataGridColumnHelper
{
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.RegisterAttached("Header", typeof(string), typeof(DataGridColumnHelper),
new PropertyMetadata(OnHeaderPropertyChanged));
private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string header = GetHeader(d);
var dataGridColumn = d as DataGridColumn;
if (dataGridColumn == null)
{
return;
}
dataGridColumn.Header = GetHeader(dataGridColumn);
}
public static string GetHeader(DependencyObject obj)
{
return (string)obj.GetValue(HeaderProperty);
}
public static void SetHeader(DependencyObject obj, string value)
{
obj.SetValue(HeaderProperty, value);
}
}
As you can see it is really simple, I am trying to overcome the limitation that the Header Property in the DataGridColumn class cannot be bound.
This XAML works as expected...
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="User Name"/>
However this XAML throws an error...(Specifically: {System.Windows.Markup.XamlParseException: AG_E_PARSER_PROPERTY_NOT_FOUND [Line: 224 Position: 112]
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
....})
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding Resources.UserNameListViewHeading, Source={StaticResource Labels}}"/>
Just for experimentation I attached this property (with the binding syntax above) to a DataGrid
and checked the DataGridColumnHelper.Header
property in the OnHeaderPropertyChanged
method and the value was correct (and an exception wasn't thrown)
It is my understanding that the object that the AttachedProperty is attached to must be a DependencyProperty
. Looking through Reflector, DataGridColumn
(from which DataGridTextColumn
derives) derives from DependencyProperty
.
Can somebody please shed some light on this? I am trying to Localize our application, and I am having trouble with the DataGrid. I am sure I can do this in code-behind, but I am trying to avoid that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
克里斯,问题很简单,这行不通,因为 DataGridTextColumn 与可视化树“分离”。您的 DataGridTextColumn 对象植根于 DataGrid 的 Columns 集合 - 请参阅间接寻址。因此,即使附加属性也不会按您的预期工作。现在有一种方法可以使用我称之为附加绑定的东西来完成所有这些工作,请参阅:
http://www.orktane.com/Blog/post/2009/09/29/Introducing-nRouteToolkit-for-Silverlight-(部分- I).aspx
只需记住使用 VisualTree 中的内容附加绑定属性(这样持有列的网格就可以了。)
希望这会有所帮助。
Chris, the problem is very simple, this won't work because the DataGridTextColumn is "detached" from the Visual Tree. Your DataGridTextColumn object is rooted in the Columns collection of the DataGrid - see the indirection. So even attached properties will not work as you expect. Now there is a way to make all this work using something I'm calling Attached Bindings, see:
http://www.orktane.com/Blog/post/2009/09/29/Introducing-nRouteToolkit-for-Silverlight-(Part-I).aspx
Just remember to attach the binding properties using something that is in the VisualTree (so the Grid holding the column would do just fine.)
Hope this helps.
尝试使用这个,我假设 UserName 是您视图模型中的一个属性,
我无法测试您的场景,所以我的帖子只是一个想法。可能有效,也可能无效。
Try using this, im assuming UserName is a property in your viewmodel
I cant test your scenario so my post is just an idea. Might work, might not.