【发布时间】: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