从 WPF 中的数据绑定数据网格中排除列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14842151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:29:29  来源:igfitidea点击:

Exclude columns from a databound datagrid in WPF

c#wpfdata-bindingdatagridblend

提问by forTruce

I have an ObservableCollection<SolarSystemViewModel>where SolarSystemViewModelderives from the ViewModelBase. The ViewModelBaseexposes IsInDesignModeand IsInDesignModeStaticthat are showing up in the datagrid when I bind to my ObservableCollection. How can I hide those columns from the datagrid by default without having to generate the XAML with Blend and then disable those columns manually?

我有一个ObservableCollection<SolarSystemViewModel>地方SolarSystemViewModel从导出ViewModelBase。该ViewModelBase自曝IsInDesignModeIsInDesignModeStatic所显示出来的DataGrid,当我绑定到我的ObservableCollection。如何在默认情况下从数据网格中隐藏这些列,而不必使用 Blend 生成 XAML,然后手动禁用这些列?

Thanks.

谢谢。

Example: enter image description here

例子: 在此处输入图片说明

回答by Tara

I've been searching for an answer to this question too. Here's a pretty nice solution:

我也一直在寻找这个问题的答案。这是一个非常好的解决方案:

Bind the property "OnAutoGeneratingColumn" of your DataGrid like this (attention the XAML is not 100% complete):

像这样绑定 DataGrid 的属性“OnAutoGeneratingColumn”(注意 XAML 不是 100% 完整):

<DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn" />

And in your CodeBehind:

在你的 CodeBehind 中:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    PropertyDescriptor propertyDescriptor = (PropertyDescriptor)e.PropertyDescriptor;
    e.Column.Header = propertyDescriptor.DisplayName;
    if (propertyDescriptor.DisplayName == "IsInDesignMode")
    {
        e.Cancel = true;
    }
}

"e.Cancel = true;"will prevent the current column from being generated. That's how you can easily exclude columns from the DataGrid.

"e.Cancel = true;" 将阻止生成当前列。这就是您可以轻松地从 DataGrid 中排除列的方法。

回答by Kentonbmax

This is quite simple once I thought about it. MS left us in good shape because we can access the properties of the ObservableCollection's T value using the Path. This will update the observable collection with results from the datagrid. I use prism for the update/insert side using an ICommand that is binded to a save button. My query using SQLite supports insert and update in the same method which makes life easy. For clarification I'm following MVVM using Unity and Prism.

一旦我想到它,这很简单。MS 让我们保持良好状态,因为我们可以使用 Path 访问 ObservableCollection 的 T 值的属性。这将使用来自数据网格的结果更新可观察集合。我使用绑定到保存按钮的 ICommand 将棱镜用于更新/插入端。我使用 SQLite 的查询支持以相同的方法插入和更新,这让生活变得轻松。为了澄清起见,我正在使用 Unity 和 Prism 关注 MVVM。

<DataGrid Name="_dgProtocolSource" HorizontalAlignment="Left" Margin="-161,-61,-162,-163" AutoGenerateColumns="False" VerticalAlignment="Top" Width="365" Height="224" SelectionMode="Single" ItemsSource="{Binding OCSource, Mode=TwoWay}" CanUserAddRows="True" CanUserDeleteRows="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="ColumnName1" Binding="{Binding Path=Property1}" />
            <DataGridTextColumn Header="ColumnName2" Binding="{Binding Path=Property2}"/>
            <DataGridTextColumn Header="ColumnName3" Binding="{Binding Path=Property3}"/>
        </DataGrid.Columns>

回答by Chintana Meegamarachchi

Please try manually defining the columns.. something along the lines of.

请尝试手动定义列.. 类似的东西。

<dg:DataGrid x:Name="myDataGrid" ItemsSource="{Binding Path = SolarSystemViewModels}" AutoGenerateColumns="False">
  <dg:DataGrid.Columns>
    <dg:DataGridTextColumn Binding="{Binding FactionKills}" Header="Faction Kills" />
    <dg:DataGridTextColumn Binding="{Binding Jumps}" Header="Jumps" />
    <dg:DataGridTextColumn Binding="{Binding PodKills}" Header="Pod Kills" />
    <dg:DataGridTextColumn Binding="{Binding ShipKills}" Header="Ship Kills" />
  </dg:DataGrid.Columns>
</dg:DataGrid>

You dont have to use blend for this, just use VS XAML editor

您不必为此使用混合,只需使用 VS XAML 编辑器