2016-04-24 41 views
0

我試過幾件事情,但觸發器中的AlternationIndex根本不起作用。WPF DataGrid交替行背景不起作用

如果我使用

<DataGrid x:Name="dataGrid" Margin="10,40,10,10" ItemsSource="{Binding}" 
    AlternatingRowBackground="DimGray" AlternationCount="1" 

它的工作原理,但隨後IsMouseOver觸發器不會對交行工作。

所以我做大家的事情是寫在這裏:

<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}"> 
     <Setter Property="Height" Value="22"/> 
     <Style.Triggers> 
      <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
       <Setter Property="Background" Value="#FF574F4F"/> 
      </Trigger> 
      <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
       <Setter Property="Background" Value="AliceBlue"/> 
      </Trigger> 
      <Trigger Property="IsMouseOver" Value="true"> 
       <Setter Property="Background" Value="#FF8B1515" /> 
      </Trigger> 
     </Style.Triggers> 
... 
... 
<DataGrid x:Name="dataGrid" Margin="10,40,10,10" ItemsSource="{Binding}" IsReadOnly="True" 
        ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}" 
        RowHeaderStyle="{StaticResource DataGridRowHeaderStyle}" 
        CellStyle="{StaticResource DataGridCellStyle}" 
        RowStyle="{StaticResource DataGridRowStyle}".... 


     <DataGrid.Columns> 
      <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
      <DataGridTextColumn Header="Description" Binding="{Binding Description}"/> 
      <DataGridTextColumn Header="Author" Binding="{Binding Author}"/> 
     </DataGrid.Columns> 

與該代碼的艾莉斯藍不會出現。試了一切,但我不知道爲什麼它什麼都不做。 AlternationIndex 0作爲背景,但不是1,2或3. 作爲綁定源我已經使用了一個DataTable 只是一個簡單的類與一些方法。我從Java 1.6 SWT的到來,那花哨的東西是我的全部新

table.Columns.Add("Name", typeof(string)); 
table.Columns.Add("Description", typeof(string)); 
table.Columns.Add("Author", typeof(string));  
.... 
table.Rows.Add(name, description, author); 

從上面代碼返回值設置的DataContext

dataGrid.DataContext = dTable.getTable(); 

有沒有人一個想法,爲什麼它不」工作..它真的讓我瘋狂。

回答

1

你也必須設置DataGrid的財產AlternationCount =「2」

+0

哦..男人爲你擁抱! :D僅對AlternatingRowBackground使用了AlternationCount,但對Triggers不使用。謝謝 :) –