数据网格中的触发器

时间:2013-06-24 09:31:47

标签: c# wpf xaml triggers

我对此WPF-xaml代码有疑问。没有使用xaml很多,但设法得到我想要的代码。但我觉得这根本不是好习惯。首先,我想根据某个单元格中的某些值将行从红色更改为绿色或灰色。我是否真的必须为每个单元保留一个触发器,或者无论如何都要在整行上执行相同的触发器?我还想在选择行时设置特定颜色,但样式似乎不支持“IsSelected”......

<DataGrid AutoGenerateColumns="False" IsReadOnly="True" HorizontalAlignment="Stretch" Margin="200,50,5,5" ItemsSource="{Binding}" Name="dataGrid1" VerticalAlignment="Stretch" EnableRowVirtualization="True" SelectionChanged="dataGrid1_SelectionChanged" MouseDoubleClick="dataGrid1_MouseDoubleClick">
        <DataGrid.Columns>
            <DataGridTextColumn Width="1*" Header="File name" Binding="{Binding Path=Filename}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="#68FF0000" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Errorcode}" Value="0">
                                <Setter Property="Background" Value="LightGreen"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Filename}" Value="File not created">
                                <Setter Property="Background" Value="LightGray" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Width="1*" Header="Weight" Binding="{Binding Path=Info1}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="#68FF0000" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Errorcode}" Value="0">
                                <Setter Property="Background" Value="LightGreen" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Filename}" Value="File not created">
                                <Setter Property="Background" Value="LightGray" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Width="1*" Header="Rfid" Binding="{Binding Path=Info1}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="#68FF0000" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Errorcode}" Value="0">
                                <Setter Property="Background" Value="LightGreen" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Filename}" Value="File not created">
                                <Setter Property="Background" Value="LightGray" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Width="1*" Header="Date/Time" Binding="{Binding Datetime, StringFormat=\{0:yyyy.MM.dd HH:mm:ss\}}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="#68FF0000" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Errorcode}" Value="0">
                                <Setter Property="Background" Value="LightGreen" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Filename}" Value="File not created">
                                <Setter Property="Background" Value="LightGray" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Width="1*" Header="Error code" Binding="{Binding Path=Errorcode}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="#68FF0000" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Errorcode}" Value="0">
                                <Setter Property="Background" Value="LightGreen" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Filename}" Value="File not created">
                                <Setter Property="Background" Value="LightGray" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

1 个答案:

答案 0 :(得分:2)

我对DataGrid不是很有经验,所以这可能不是最好的选择,但我认为你可以通过设置Row的样式而不是单个列来实现你所追求的目标。 :

    <DataGrid <!-- Your settings here -->>
        <!-- Your column definitions here -->
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Errorcode}"  Value="0">
                        <Setter Property="Background" Value="LightGreen" />
                        <!-- Other Setters -->
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Filename}" Value="File not created">
                        <Setter Property="Background" Value="LightGray" />
                    </DataTrigger>

                    <!-- Other Triggers -->
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Red" />
                        <!-- Other Setters -->
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
祝你好运。