2010-11-27 26 views
2

如何爲WPF ListView設置備用行顏色。 如果我只有一個列表,我可以在XAML中設置,但在我的情況下,需要根據列表更改備用行顏色。 例如,我有3名差異列表...
1)爲了用公司名稱,
2)爲了按行業
3)爲了按市場價值
每個列表應該有自己的備用行顏色。
我怎樣才能做到這一點(在C#或XAML文件)。任何意見/建議,將aprreciatedWPF列表查看3個不同列表的不同交替行顏色..?

回答

6

這應該工作,不管你做什麼的清單,因爲ItemsControl.AlternationIndex是一個依賴屬性,應該得到更新:

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:WpfApplication1" 
Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <Style TargetType="ListBoxItem"> 
     <Style.Triggers> 
      <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
       <Setter Property="Background" Value="AliceBlue" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <ListBox Name="bob" AlternationCount="2"> 
     <ListBoxItem Content="Herald"/> 
     <ListBoxItem Content="Kumar" /> 
     <ListBoxItem Content="Bill" /> 
     <ListBoxItem Content="Dudley" /> 
     <ListBoxItem Content="Jack" /> 
    </ListBox> 
    <Button Click="Button_Click">boo</Button> 
</StackPanel> 

代碼隱藏測試修改項目:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
    Dim item1 As ListBoxItem = bob.Items(4) 
    bob.Items.Remove(item1) 
    bob.Items.Insert(0, item1) 
End Sub 
+0

如果你的意思,你想要一個不同的R ow顏色使用取決於排序然後我想你會想要使用MultiTrigger代替,其中一個觸發屬性是交替索引和一個屬性連接到一個屬性,指示正在使用什麼類型。 – 2010-11-28 01:45:11