WPF DataGrid触发单元格内容

时间:2015-06-17 08:25:43

标签: c# wpf datagrid triggers

我的datagrid包含的值来自stored procedure。所有值都设置为Bold FontWeight

我想在单元格内容等于0时使文本正常。

如何使用触发器执行此操作?

我已经像下面那样完成了它,但它没有工作:

<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>

3 个答案:

答案 0 :(得分:9)

您无法以这种方式访问​​DataGridCell.Content,而是根据您的DataTrigger使用DataGrid.SelectedItem.YourProperty

    <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>

修改

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

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

的Xaml:

<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>

转换器:

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();
    }
}

答案 1 :(得分:2)

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

private int selectionStart;
private int selectionLength;

private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
    var textBox = sender as TextBox;
    selectionStart = textBox.SelectionStart;
    selectionLength = textBox.SelectionLength;
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (selectionLength > 0)
    {
        var textBox = sender as TextBox;
        textBox.SelectionStart = selectionStart;
        textBox.SelectionLength = selectionLength;
    }
}

您可以在TextBox的FontWeight上添加一个绑定,并使用与Text if自身关联的转换器。

答案 2 :(得分:1)

你可以这样做 -

<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>