Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么Browsable(false)不在DataGrid中隐藏列_C#_Wpf_Visual Studio_Datagrid - Fatal编程技术网

C# 为什么Browsable(false)不在DataGrid中隐藏列

C# 为什么Browsable(false)不在DataGrid中隐藏列,c#,wpf,visual-studio,datagrid,C#,Wpf,Visual Studio,Datagrid,在我的WPF应用程序中,我想通过向某些属性添加[Browsable(false)]来隐藏DataGrid中绑定ItemsSource的列 但是,无论是否可浏览(false),所有列都是可见的 我的模型: public class Room : INotifyPropertyChanged { private int id; ... [Browsable(false)] public int Id { get {

在我的WPF应用程序中,我想通过向某些属性添加[Browsable(false)]来隐藏DataGrid中绑定ItemsSource的列 但是,无论是否可浏览(false),所有列都是可见的

我的模型:

public class Room : INotifyPropertyChanged
{
    private int id;
  ...
    [Browsable(false)]
    public int Id
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
            this.OnPropertyChanged("Id");
        }
    }
    ...
    public Room()
    {
    }
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler propertyChangedEventHandler = this.PropertyChanged;
        if (propertyChangedEventHandler != null)
        {
            propertyChangedEventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
视图:



如何使用Browsable(false)隐藏列?

恐怕您不能。
Browsable
属性只影响可视化设计器的属性视图,在运行时不起作用


有关更多信息,请检查不要调用
this.OnPropertyChanged(“Id”)

您可以通过连接DataGrid上的AutoGeneratingColumn事件来隐藏它们(请参阅)

公共类DataGridHideBrowsableFalseBehavior:Behavior
{
受保护的覆盖无效附加()
{
AssociatedObject.AutoGeneratingColumn+=AssociatedObject_AutoGeneratingColumn;
base.onatached();
}
private void Associated object_AutoGeneratingColumn(对象发送方,DataGridAutoGeneratingColumnEventArgs e)
{
if(((PropertyDescriptor)e.PropertyDescriptor).IsBrowsable==false)
e、 取消=真;
}
}
<DataGrid Grid.Row="1" ItemsSource="{Binding Rooms}" SelectedItem="{Binding SelectedRoom, Mode=TwoWay}" />
public class DataGridHideBrowsableFalseBehavior : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        AssociatedObject.AutoGeneratingColumn += AssociatedObject_AutoGeneratingColumn;
        base.OnAttached();
    }

    private void AssociatedObject_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (((PropertyDescriptor)e.PropertyDescriptor).IsBrowsable == false)
            e.Cancel = true;
    }
}


<DataGrid
    ItemsSource="{Binding Path=DataGridSource, Mode=OneWay}"
    AutoGenerateColumns="true">
        <i:Interaction.Behaviors>
            <behaviors:DataGridHideBrowsableFalseBehavior>
             </behaviors:DataGridHideBrowsableFalseBehavior>
        </i:Interaction.Behaviors>
</DataGrid>