如果值大于,XAML Datagrid 设置 rowStyle

发布于 2025-01-19 18:02:26 字数 1429 浏览 1 评论 0原文

我有一个带有3列的数据杂志。

现在,我想有条件地更改行的亮点。 类似:如果第2列中的值大于xx将行颜色更改为红色。

我已经尝试了一些事情,但是没有成功:

<DataGrid Name="DataGrid1" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" AlternatingRowBackground="LightGray"  ItemsSource="{Binding}" AutoGenerateColumns="False" FontSize="18" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserAddRows="False">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Steckzyklen}" Value="&lt; 20">
                        <Setter Property="Background" Value="#FFFF4848"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Steckplatz"  Binding="{Binding Name}" Width="200"/>
            <DataGridTextColumn Header="Steckzyklen"  Binding="{Binding Steckzyklen}" Width="200"/>
            <DataGridTextColumn Header="Austauschdatum"  Binding="{Binding Austauschdatum}" Width="200"/>
        </DataGrid.Columns>
    </DataGrid>

在这一行中:dataTrigger binding =“ {binding steckzyklen}” value =“&amp; lt 20” 我尝试用&amp; lt做这件事,但没有成功。

有人知道如何使它起作用?最好仅通过更改XAML中的内容。

提前致谢!

I've got a Datagrid with 3 columns.

Now i want to change the highlight of the row conditionally.
like: if the value in column 2 is greater than XX change row color to Red.

I've already tried something, but without success:

<DataGrid Name="DataGrid1" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" AlternatingRowBackground="LightGray"  ItemsSource="{Binding}" AutoGenerateColumns="False" FontSize="18" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserAddRows="False">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Steckzyklen}" Value="< 20">
                        <Setter Property="Background" Value="#FFFF4848"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Steckplatz"  Binding="{Binding Name}" Width="200"/>
            <DataGridTextColumn Header="Steckzyklen"  Binding="{Binding Steckzyklen}" Width="200"/>
            <DataGridTextColumn Header="Austauschdatum"  Binding="{Binding Austauschdatum}" Width="200"/>
        </DataGrid.Columns>
    </DataGrid>

In this line: DataTrigger Binding="{Binding Steckzyklen}" Value="< 20"
I tried ti do it with the < but without succes.

Someone knows how to make it work? Preferably by only changing something in the XAML.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2025-01-26 18:02:26

您可以按以下方式更改样式:

创建一个新的转换器:

    public class IsEqualOrLessThanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int intValue = (int)value;
            int compareToValue = Int32.Parse(parameter.ToString() ?? string.Empty);

            return intValue <= compareToValue;
        }

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

然后:

<Style TargetType="DataGridRow">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Steckzyklen, Converter={StaticResource IsEqualOrLessThanConverter}, ConverterParameter=20}" Value="True">    
            <Setter Property="Foreground" Value="#FFFF4848"/>
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

You can change the Style as follows:

Create a new Converter:

    public class IsEqualOrLessThanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int intValue = (int)value;
            int compareToValue = Int32.Parse(parameter.ToString() ?? string.Empty);

            return intValue <= compareToValue;
        }

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

And then:

<Style TargetType="DataGridRow">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Steckzyklen, Converter={StaticResource IsEqualOrLessThanConverter}, ConverterParameter=20}" Value="True">    
            <Setter Property="Foreground" Value="#FFFF4848"/>
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文