如何绑定DataGridColumn.Visibility? [英] How to bind DataGridColumn.Visibility?

查看:202
本文介绍了如何绑定DataGridColumn.Visibility?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的帖子一个问题:

I have an issue similar to the following post:

<一个href=\"http://stackoverflow.com/questions/983272/silverlight-datagridtextcolumn-binding-visibility\">Silverlight DataGridTextColumn绑定能见度

我需要有一个Silverlight的DataGrid中的列中都看得到/折叠基于一个ViewModel内的值。要做到这一点,我试图绑定可见性属性设置为视图模型。不过很快我就发现可见性属性不是一个DependencyProperty的,因此它无法绑定。

I need to have a Column within a Silverlight DataGrid be visibile/collapsed based on a value within a ViewModel. To accomplish this I am attempting to Bind the Visibility property to a ViewModel. However I soon discovered that the Visibility property is not a DependencyProperty, therefore it cannot be bound.

要解决这个问题,我试图子类我自己DataGridTextColumn。有了这个新的类,我创建了一个的DependencyProperty,最终推动改变DataGridTextColumn.Visibility财产。这个效果很好,如果我不数据绑定。我数据绑定到我的新属性的那一刻,它失败,用AG_E_PARSER_BAD_PROPERTY_VALUE例外。

To solve this, I attempted to subclass my own DataGridTextColumn. With this new class, I have created a DependencyProperty, which ultimately pushes the changes to the DataGridTextColumn.Visibility property. This works well, if I don't databind. The moment I databind to my new property, it fails, with a AG_E_PARSER_BAD_PROPERTY_VALUE exception.

public class MyDataGridTextColumn : DataGridTextColumn
{
    #region public Visibility MyVisibility

    public static readonly DependencyProperty MyVisibilityProperty =
        DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(MyDataGridTextColumn), new PropertyMetadata(Visibility.Visible, OnMyVisibilityPropertyChanged));

    private static void OnMyVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var @this = d as MyDataGridTextColumn;

        if (@this != null)
        {
            @this.OnMyVisibilityChanged((Visibility)e.OldValue, (Visibility)e.NewValue);
        }
    }

    private void OnMyVisibilityChanged(Visibility oldValue, Visibility newValue)
    {
        Visibility = newValue;
    }

    public Visibility MyVisibility
    {
        get { return (Visibility)GetValue(MyVisibilityProperty); }
        set { SetValue(MyVisibilityProperty, value); }
    }

    #endregion public Visibility MyVisibility
}

下面是XAML的一个小片段。

Here is a small snippet of the XAML.

<DataGrid ....>
    <DataGrid.Columns>
        <MyDataGridTextColumn Header="User Name"
                              Foreground="#FFFFFFFF"
                              Binding="{Binding User.UserName}"
                              MinWidth="150"
                              CanUserSort="True"
                              CanUserResize="False"
                              CanUserReorder="True"
                              MyVisibility="{Binding Converter={StaticResource BoolToVisibilityConverter}, Path=ShouldShowUser}"/>
        <DataGridTextColumn .../>
    </DataGrid.Columns>
</DataGrid>

一对夫妇重要的事实。


  • 转换器确实是在上面的本地资源定义。

  • 转换器是正确的,它被用于许多其他地方在解决方案中。

  • 如果我更换{结合}语法与坍塌的MyVisibility属性列事实上确实消失了。

  • 如果我创建一个新的DependencyProperty(即字符串美孚),并将绑定到它,我收到AG_E_PARSER_BAD_PROPERTY_VALUE例外了。

没有任何人有任何想法,为什么这不工作?

Does anybody have any ideas as to why this isn't working?

推荐答案

下面是我想出了使用一个小黑客的解决方案。

Here's the solution I've come up with using a little hack.

首先,你需要从DataGrid中继承。

First, you need to inherit from DataGrid.

public class DataGridEx : DataGrid
{
    public IEnumerable<string> HiddenColumns
    {
        get { return (IEnumerable<string>)GetValue(HiddenColumnsProperty); }
        set { SetValue(HiddenColumnsProperty, value); }
    }

    public static readonly DependencyProperty HiddenColumnsProperty =
        DependencyProperty.Register ("HiddenColumns", 
                                     typeof (IEnumerable<string>), 
                                     typeof (DataGridEx),
                                     new PropertyMetadata (HiddenColumnsChanged));

    private static void HiddenColumnsChanged(object sender,
                                             DependencyPropertyChangedEventArgs args)
    {
        var dg = sender as DataGrid;
        if (dg==null || args.NewValue == args.OldValue)
            return;

        var hiddenColumns = (IEnumerable<string>)args.NewValue;
        foreach (var column in dg.Columns)
        {
            if (hiddenColumns.Contains ((string)column.GetValue (NameProperty)))
                column.Visibility = Visibility.Collapsed;
            else
                column.Visibility = Visibility.Visible;
        }
    }
}

DataGridEx 类增加了一个新的DP用于隐藏基础上的 x列:名称 的DataGridColumn 及其后代

The DataGridEx class adds a new DP for hiding columns based on the x:Name of a DataGridColumn and its descendants.

要在您的XAML是:

<my:DataGridEx x:Name="uiData"
               DataContext="{Binding SomeDataContextFromTheVM}"
               ItemsSource="{Binding Whatever}"
               HiddenColumns="{Binding HiddenColumns}">
    <sdk:DataGridTextColumn x:Name="uiDataCountOfItems">
                            Header="Count"
                            Binding={Binding CountOfItems}"
    </sdk:DataGridTextColumn>
</my:DataGridEx>

您需要将这些添加到您的视图模型或任何数据上下文使用。

You need to add these to your ViewModel or whatever data context you use.

private IEnumerable<string> _hiddenColumns;
public IEnumerable<string> HiddenColumns
{
    get { return _hiddenColumns; }
    private set
    {
        if (value == _hiddenColumns)
            return;

        _hiddenColumns = value;
        PropertyChanged (this, new PropertyChangedEventArgs("HiddenColumns"));
    }
}

public void SomeWhereInYourCode ()
{
    HiddenColumns = new List<string> {"uiDataCountOfItems"};
}

要取消隐藏,只需从列表中删除相应的名称或重新创建它没有取消隐藏名称。

To unhide, you only need to remove the corresponding name from the list or recreate it without the unhidden name.

这篇关于如何绑定DataGridColumn.Visibility?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆