【问题标题】:How do I change what shows to not match the ItemsSource in DataGrid?如何更改显示内容与 DataGrid 中的 ItemsSource 不匹配?
【发布时间】:2014-02-15 20:00:20
【问题描述】:

我有一个带有 DataContext 的 DataGrid 作为具有许多属性的对象列表。

所有列都绑定到所述对象类型的不同属性。

有一个 text 属性,我只关心另一个 bool 属性是否设置为“true” - 当它为“false”时,我根本不希望 text 属性显示。

你有什么办法建议这样做吗?

我在想一些类似的事情:

foreach (var cur in DG.ItemsSource) {
 if (!cur.BoolThatSignifiesIfICareAboutOtherProperty) {
  //some code that will make the cell for cur.PropertyIMayOrMayNotCareAbout
  //appear empty even though that property does have a value that the column
  //is bound to
 }
}

** 编辑 **

有人建议我使用数据触发器。所以这是我尝试过的,但它也不起作用。它也没有给出任何错误,所以我不知道我做错了什么。

这是我目前所拥有的。但是,它不起作用。请让我知道我做错了什么:

 <DataGridTextColumn Header="EndDate" Binding="{Binding Path=EndDate, Converter={StaticResource DT2D}}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=DoICare}" Value="False">
                                <Setter Property="Content" Value=" " />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

【问题讨论】:

  • 您正在寻找DataTrigger
  • @HighCore 所以我以前从未使用过 DataTrigger。这是我认为最适合我想要的东西的东西,但它不起作用。它实际上并没有使单元格显示为空 - 它仍然显示数据: - 我将它作为对问题的编辑发布,因为我没有意识到在这里点击“输入”会发布评论而不是让我输入更多

标签: wpf datagrid itemssource


【解决方案1】:

所以这就是我解决它的方法 - 我只是有一些标签错误:

 <DataGridTextColumn Header="EndDate" Binding="{Binding Path=EndDate, Converter={StaticResource DT2D}}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=DoICare}" Value="False">
                                <Setter Property="Visibility" Value="Hidden" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2011-10-26
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多