2017-02-16 160 views
0

我新的WPF,我有以下的列表視圖XAML代碼:如何動態地更改WPF listview行背景顏色?

<ListView x:Name="listView1" ItemsSource="{Binding Processes1}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="470" Margin="10,95,150,6" Width="565" SelectionChanged="NetscalerCfgView_listView1_SelectionChanged"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Line" DisplayMemberBinding="{Binding srcCfgLineNum}"/> 
      <GridViewColumn Header="Source Config" DisplayMemberBinding="{Binding srcConfigText}"/> 
     </GridView> 
    </ListView.View> 
</ListView> 

我有類SrcListViewInfo我在列表視圖正在顯示:

public class SrcListViewInfo 
{ 
    public int srcCfgLineNum { get; set; } 
    public string srcConfigText { get; set; } 
} 

我在窗戶宣佈它加載事件是這樣的:

public ObservableCollection<SrcListViewInfo> processes1 = null; 
processes1 = new ObservableCollection<SrcListViewInfo>(); 

我想動態地在不同的情況下在不同的情況下在不同的函數中動態地着色行背景,例如:

case DiffResultSpanStatus.DeleteSource: 
    for (i = 0; i < drs.Length; i++) 
    { 
     SrcListViewInfo newInfo = new SrcListViewInfo(); 
     newInfo.BackgroundColor = new SolidColorBrush(Colors.Red); 
     // newInfo.BackgroundColor = Brushes.Red; 
     newInfo.srcCfgLineNum = cnt; 
     newInfo.srcConfigText = ((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line; 
     // newInfo.BackgroundColor = Brushes.Red; << want to set the color like this. 

我已經嘗試過可靠的刷子,但它似乎沒有正常工作。

回答

0

一個選項

ListView.ItemTemplate

<ListView DataContext="{Binding}" ItemsSource="{Binding Models}" AlternationCount="3" > 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Name }"/> 
        <TextBlock Text="{Binding Desc }"/> 
        <StackPanel.Background> 
          <MultiBinding Converter="{StaticResource BackConverter}"> 
           <Binding /> 
           <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}"/> 
          </MultiBinding> 
        </StackPanel.Background> 
       </StackPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 


public class BackConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     // write your logic (You have the model and the view model) 

     var index = ((ItemsControl)values[1]).Items.IndexOf(values[0]); 
     if (index % 2 == 0) 
      return Brushes.Gray; 
     return Brushes.White; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

另一選項

ListView.ItemContainerStyle

使用 ItemsControl.AlternationIndex使用

編輯

public MainWindow() 
{ 
    InitializeComponent(); 
    lv.ItemsSource = new List<string> { "a", "b", "c", "d", "e" }; 
} 
+0

謝謝。我認爲第二個是更好的方法。你能告訴我什麼是相應的代碼? – PKB85

+0

在這裏我使用了MVVM方法(谷歌它)。但是,您可以簡單地設置ItemsSource並查看結果。我添加了一個非常簡單的例子。 – Ron

+0

它工作正常,但靜態。我只想爲特定條件(在單獨的函數下)設置顏色,而不是默認設置。 – PKB85

0

經過一番google搜索,我發現我自己的解決方案,我用Listview.ItemsSource和源我使用List與ListViewItems然後我可以設置列表中指定的ListViewItem的背景,只是刷新列表視圖。

XAML:

<ListView x:Name="listView" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1"> 
       <ListView.View> 
        <GridView> 
         <GridViewColumn Header="IP" DisplayMemberBinding="{Binding IP}" Width="Auto"/> 
         <GridViewColumn Header="PING" DisplayMemberBinding="{Binding Ping}" Width="Auto"/> 
         <GridViewColumn Header="Host Name" DisplayMemberBinding="{Binding DNS}" Width="Auto"/> 
         <GridViewColumn Header="Mac" DisplayMemberBinding="{Binding MAC}" Width="Auto"/> 
         <GridViewColumn Header="Výrobce" DisplayMemberBinding="{Binding Manufacturer}" Width="Auto"/> 
        </GridView> 
       </ListView.View> 
      </ListView> 

填充的ListView與灰色背景資料:

List<ListViewItem> ITEMS = new List<ListViewItem>(); 
private void button_Click(object sender, RoutedEventArgs e) 
{ 
    for (int i = 1; i < 20; i++) 
    { 
     ListViewItem OneItem = new ListViewItem(); 
     OneItem.Background = Brushes.LightGray; 
     OneItem.Content = new Device() { IP = "1.1.1.1", Ping = "30ms", DNS = "XYZ", MAC = "2F:3C:5F:41:F9", Manufacturer = "Intel" }; 
     ITEMS.Add(OneItem); 
     listView.ItemsSource = ITEMS; 
    } 
    listView.Items.Refresh(); 
} 
public class Device 
{ 
    public string IP { get; set; } 
    public string Ping { get; set; } 
    public string DNS { get; set; } 
    public string MAC { get; set; } 
    public string Manufacturer { get; set; } 
} 

的行更改顏色創建方法:

private void ChangeRowColor(int RowIndex,SolidColorBrush NewBackground) 
{ 
    ITEMS[RowIndex].Background = NewBackground; 
    listView.Items.Refresh(); 
} 

並使用它:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    ChangeRowColor(4, Brushes.Green); 
} 
+0

如果我在列表中使用我的自定義班級名稱? 它仍然會工作? –

1

您可以風格XAML直接ListViewItem的,

例子:

假設你的 「名稱」 變量是一個字符串,你可以嘗試

<ListView Name="whatever"> 
    <ListView.Resources> 
    <Style TargetType="{x:Type ListViewItem}"> 
     <Style.Triggers> 
     <DataTrigger Binding="{Binding Name}" 
         Value="John"> 
      <Setter Property="Background" 
        Value="Red" /> 
     </DataTrigger> 
     </Style.Triggers> 
    </Style> 
    </ListView.Resources> 
.... 

現在,任何的ListView一行「名稱「價值約翰」將有一個「紅色」背景