我新的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.
我已经尝试过可靠的刷子,但它似乎没有正常工作。
谢谢。我认为第二个是更好的方法。你能告诉我什么是相应的代码? – PKB85
在这里我使用了MVVM方法(谷歌它)。但是,您可以简单地设置ItemsSource并查看结果。我添加了一个非常简单的例子。 – Ron
它工作正常,但静态。我只想为特定条件(在单独的函数下)设置颜色,而不是默认设置。 – PKB85