我的问题是,我希望我的WPF ListView显示listviewitem在不同的颜色,根据项目绑定到他们。下面是重现这个问题的所有代码。这款应用的小部件要么"有货",要么没有。如果它们在库存中,它们应该在ListView中有绿色文本,否则是红色。颜色是由我的BoolToColorConverter(转换小部件)处理的。stock to Colors。绿色或颜色,红色)。断点使我确信Convert()正在被击中。我可能做了一些明显的错误,但我在一个损失,因为硬编码的前景颜色工作如预期。提前谢谢。
MainWindow.xaml
<Window x:Class="ListViewItemColors.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListViewItemColors" Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:WidgetsViewModel />
</Window.DataContext>
<Window.Resources>
<local:BoolToColorConverter x:Key="BoolToColor" />
</Window.Resources>
<DockPanel >
<ListView ItemsSource="{Binding Path=Widgets }">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Widget Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" Foreground="{Binding Path=InStock, Converter={StaticResource BoolToColor}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="50" Header="Size" DisplayMemberBinding="{Binding Size}"></GridViewColumn>
<GridViewColumn Width="75" Header="In Stock?" DisplayMemberBinding="{Binding InStock}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
BoolToColorConverter.cs
public class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = (bool)value;
return val ? Colors.Green : Colors.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
WidgetsViewModel.cs
public class WidgetsViewModel
{
public WidgetsViewModel()
{
Widgets = new Widgets
{
new Widget {InStock = true, Name = "Flipper", Size = 10},
new Widget {InStock = false, Name = "Gizmo", Size = 6},
new Widget {InStock = true, Name = "Gizmo", Size = 8},
new Widget {InStock = true, Name = "Whirlygig", Size = 15},
new Widget {InStock = false, Name = "Gutter", Size = 1},
new Widget {InStock = false, Name = "Gutter", Size = 2}
};
}
public Widgets Widgets { get; set; }
}
Widget.cs
public class Widget : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged("Name");
}
}
private int _size;
public int Size
{
get { return _size; }
set
{
if (_size == value) return;
_size = value;
OnPropertyChanged("Size");
}
}
private bool _inStock;
public bool InStock
{
get { return _inStock; }
set
{
if (_inStock == value) return;
_inStock = value;
OnPropertyChanged("InStock");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
Widgets.cs
public class Widgets : ObservableCollection<Widget> { }
TextBlock。前景属性取Brush
,而不是Color
。更改转换器,使其返回SolidColorBrush
,例如:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = (bool)value;
return new SolidColorBrush(val ? Colors.Green : Colors.Red);
}
这对我有用:
enter code here
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Foreground"
Value="{Binding FColor,Mode=OneWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ColorConverter}}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Category"
DisplayMemberBinding="{Binding CatDesc,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
Width="450"/>
</GridView>
</ListView.View>