WPF DataGrid 触发单元格内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30886120/
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 13:12:44  来源:igfitidea点击:

WPF DataGrid Trigger on cell content

c#wpfdatagridtriggers

提问by Galma88

I have a datagridthan contains value comes from a stored procedure. All values are set Boldas FontWeight.

我有一个datagrid比包含值来自一个stored procedure. 所有值都设置BoldFontWeight

I'd like to make the text normal when the cell content is equal to 0.

当单元格内容等于0时,我想让文本正常。

How can I do that with a trigger?

我怎么能用触发器做到这一点?

I've done it like below but it's not working:

我已经像下面那样做了,但它不起作用:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
            <Setter Property="FontWeight" Value="Bold" />
            <Style.Triggers>
                <Trigger Property="Content" Value="0">
                    <Setter Property="FontWeight" Value="Normal"/>
                </Trigger>
            </Style.Triggers>
    </Style>
</DataGrid.CellStyle>

回答by Bahman_Aries

You can't access DataGridCell.Contentthat way, use a DataTriggerinstead based on your DataGrid.SelectedItem.YourPropertylike this:

您无法以DataGridCell.Content这种方式访问,请DataTrigger根据您的DataGrid.SelectedItem.YourProperty喜好使用 a :

    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="FontWeight" Value="Bold" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding YourProperty}" Value="0">
                    <Setter Property="FontWeight" Value="Normal"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>

EDIT:

编辑:

Assuming your DataGridColumnsare text-based then you can use an IValueConverteras below:

假设您DataGridColumns是基于文本的,那么您可以使用IValueConverter如下:

Note that if some of data-grid columns are not text-based, this solution still works for those columns which are.

请注意,如果某些数据网格列不是基于文本的,此解决方案仍然适用于那些基于文本的列。

Xaml:

Xml:

<Window.Resources>
    <local:FontWeightConverter x:Key="fontWeightConverter"/>
</Window.Resources>

...

...

    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Setters>
                <Setter Property="FontWeight" 
                       Value="{Binding RelativeSource={RelativeSource Self}, 
                       Path=Content.Text, 
                       Converter={StaticResource fontWeightConverter}}" />
            </Style.Setters>
        </Style>
    </DataGrid.CellStyle>

Converter:

转换器:

public class FontWeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (value != null && value.ToString() == "0")
            return FontWeights.Normal;
        return FontWeights.Bold;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

回答by Olaru Mircea

This is a way to define that column:

这是定义该列的一种方法:

  <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                   <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding DataBaseValue}"/>
                   </StackPanel>
             </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

You can add a binding on the FontWeightof the TextBox with a converter associated to the Text if itself.

您可以在 TextBox 的 FontWeight 上添加绑定,并使用与 Text 关联的转换器(如果其本身)。

回答by Ppp

You could do this -

你可以这样做——

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
            <Setter Property="FontWeight" Value="Bold" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Content.Text, Mode=OneWay, RelativeSource={RelativeSource Self}}" Value="0">
                    <Setter Property="FontWeight" Value="Normal"/>
                </Trigger>
            </Style.Triggers>
    </Style>
</DataGrid.CellStyle>