0
我對此WPF-xaml代碼有疑問。沒有使用XAML很多,但設法得到我想要的這個代碼。但我覺得這根本不是好習慣。首先,我想根據某個單元格中的某些值將行從紅色更改爲綠色或灰色。我是否真的必須爲每個細胞保留一個觸發器,或者無論如何都要在整行上執行相同的觸發器?我也想設置一個特定的顏色,當選擇了行,但風格似乎並不支持「IsSelected」 ......datagrid中的觸發器
<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>
Perfekt!幫助我很多!謝謝 – Nick3
很高興聽到它幫助= D – Chris