开发者

Silverlight DataGridColumn AttachedProperties

开发者 https://www.devze.com 2022-12-10 02:23 出处:网络
I am attempting to create an AttachedProperty for a DataGridColumn within Silverlight 3.0 and I am having some issues.

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消