DataGrid列可见性不起作用

时间:2016-09-02 14:44:32

标签: c# wpf xaml datagrid

我正在尝试使用ViewModel中的IsVisible属性来控制DataGridTemplateColumn的可见性。 这是xaml代码

<DataGrid>
    <DataGrid.Resources>
         <local:BindingProxy x:Key="proxy" Data="{Binding}"/>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTemplateColumn Width="32" IsReadOnly="True"
                            Visibility="{Binding  Path=Data.IsVisible,TargetNullValue=True,Source={StaticResource Proxy},Converter={StaticResource BoolToVisibilityConverter}}"   CellTemplate="{StaticResource RemoveButtonTemplate}">
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

这是我的绑定代理

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object),
                                     typeof(BindingProxy));
}

这是Viewmodel属性

public bool IsVisible
        {
            get { return true; }           
        }

每次第一次加载网格时,DataGridTemplateColumn总是被隐藏。如果我做错了,请建议。

奇怪的是,如果我在xaml中使用FrozenColumnCount,它会完美运行。

0 个答案:

没有答案
相关问题