我的麻烦在于我希望我的WPF ListView根据绑定到它们的项目以不同的颜色显示ListViewItems。以下是重现问题的所有代码。该应用程序的小部件是“有货”或不是。如果它们有库存,它们应该在ListView中有绿色文本,否则为红色。着色由我的BoolToColorConverter处理(将Widget.InStock转换为Colors.Green或Colors.Red)。断点确保我转换()。我可能做了一些明显错误的事情,但是由于前景颜色的硬编码按预期工作,我感到很茫然。提前谢谢。
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> { }
答案 0 :(得分:2)
TextBlock.Foreground属性需要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);
}
答案 1 :(得分:0)
这对我有用:
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>