Silverlight DataGrid列附加属性

发布于 2024-08-08 22:13:53 字数 2481 浏览 7 评论 0原文

我正在尝试在 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 观察,DataGridColumnDataGridTextColumn 从中派生)派生自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你而言 2024-08-15 22:13:53

克里斯,问题很简单,这行不通,因为 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.

静若繁花 2024-08-15 22:13:53

尝试使用这个,我假设 UserName 是您视图模型中的一个属性,

    <Controls:DataGridTextColumn Binding="{Binding OwnerName}"                                                                         
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding UserName}"/>

我无法测试您的场景,所以我的帖子只是一个想法。可能有效,也可能无效。

Try using this, im assuming UserName is a property in your viewmodel

    <Controls:DataGridTextColumn Binding="{Binding OwnerName}"                                                                         
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding UserName}"/>

I cant test your scenario so my post is just an idea. Might work, might not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文