【问题标题】:XAML DataGridTextColumn: display an empty cell when condition is falseXAML DataGridTextColumn:当条件为假时显示一个空单元格
【发布时间】:2017-02-27 14:18:43
【问题描述】:

我有一个文章集合,其中一些有价格,而另一些则没有。

当价格可用时,我希望我的数据网格显示它,即使它是 0。

当文章中的价格无法显示时,默认值为 0,我不希望它出现在数据网格中,并且单元格应该是只读的。

这是我尝试过的:

<DataGridTextColumn Header="PU" Binding="{Binding PU, UpdateSourceTrigger=PropertyChanged}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Mesurage.HasPrice}" Value="False">
                    <Setter Property="Focusable" Value="False"/>
                    <Setter Property="TextBlock.Text" Value="{Binding PU, StringFormat=0;;#, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

我使用了 StringFormat 来隐藏 0,但它仍然显示在我的网格中。此外,无法在触发器中修改 IsReadOnly 属性,因此我改用 Focusable 属性。

有没有办法只在 XAML 中实现想要的结果?谢谢。

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    尝试将IsEnabled 设置为false 以禁用输入

    还将Foreground 设置为Transparent:0 值将存在但不可见

    <DataTrigger Binding="{Binding Mesurage.HasPrice}" Value="False">
        <Setter Property="IsEnabled" Value="False"/>
        <Setter Property="Foreground" Value="Transparent"/>
    </DataTrigger>
    

    【讨论】:

    • 谢谢!那成功了。我确实尝试将 ForeGround 设置为 White 但是我可以在选择该行时看到该值。我不知道透明是否有价值。
    • 前景是Brush 类型的属性。它可以接受“透明”、“红色”、“绿色”、“蓝色”等字符串并将它们转换为SolidColorBrush。它可以使用其他类型的画笔,LinearGradientBrush。还有一个特殊的{x:Null} 值(xaml 中的null 等效项)。 transparent/x:Null 常用于隐藏元素
    • 感谢您的解释。我试图为您的答案投票,但我没有足够的声誉。
    猜你喜欢
    • 2011-07-03
    • 2019-01-21
    • 2015-07-30
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多