2013-06-24 75 views
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> 

回答

2

我不是很有經驗的DataGrid,所以這可能不是是最好的選擇,但我認爲你可以實現你的造型的行,而不是單獨的列後是什麼:

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

好運。

+0

Perfekt!幫助我很多!謝謝 – Nick3

+0

很高興聽到它幫助= D – Chris