绑定到 ListView 中项目的颜色 属性 不起作用
Binding to Color property of Item in ListView doesn't work
我有这个代码。 ListView 中的每个项目都包含 Label 和 CheckBox 元素。 Label 代表绑定的Lineseries item 的标题,Checkbox 绑定到LineSeries item 的IsVisible 属性。
我希望当 LineSeries 元素可见时,触发器会将(Checkbox ControlTemplate 中 Border 元素的)背景 属性 设置为 LineSeries 元素的颜色。设置为 LightGray 颜色有效,更改 IsVisible 属性 也有效,但 Border 的背景不会改变。
<ListView Name="MyListView" HorizontalAlignment="Left" Height="253" Margin="445,10,0,0" VerticalAlignment="Top" Width="72" ItemsSource="{Binding Path=Model.Series}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="2" Background="{Binding Color}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding IsVisible, Mode=TwoWay}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Border Width="15" Height="15" BorderBrush="Gray" BorderThickness="2" CornerRadius="3" />
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="{Binding Color, Converter={StaticResource OxycolorToColorConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</CheckBox.Style>
</CheckBox>
<Label Grid.Row="0" Grid.Column="1" Content="{Binding Title}" Padding="1" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
转换器:
[ValueConversion(typeof(OxyColor), typeof(Brush))]
class OxycolorToColor: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var ox = (OxyColor) value;
var color = Color.FromArgb(ox.A, ox.R, ox.G, ox.B);
var brush = new SolidColorBrush(color);
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return OxyColors.Purple;
}
}
您定义了一个 ControlTemplate 但在其中您没有使用背景 属性 因此它从未显示。
<ControlTemplate TargetType="{x:Type CheckBox}">
<Border Width="15"
Height="15"
BorderBrush="Gray"
BorderThickness="2"
CornerRadius="3"
Background="{TemplateBinding Background}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="{Binding Color, Converter={StaticResource OxycolorToColorConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
我有这个代码。 ListView 中的每个项目都包含 Label 和 CheckBox 元素。 Label 代表绑定的Lineseries item 的标题,Checkbox 绑定到LineSeries item 的IsVisible 属性。
我希望当 LineSeries 元素可见时,触发器会将(Checkbox ControlTemplate 中 Border 元素的)背景 属性 设置为 LineSeries 元素的颜色。设置为 LightGray 颜色有效,更改 IsVisible 属性 也有效,但 Border 的背景不会改变。
<ListView Name="MyListView" HorizontalAlignment="Left" Height="253" Margin="445,10,0,0" VerticalAlignment="Top" Width="72" ItemsSource="{Binding Path=Model.Series}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="2" Background="{Binding Color}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding IsVisible, Mode=TwoWay}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Border Width="15" Height="15" BorderBrush="Gray" BorderThickness="2" CornerRadius="3" />
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="{Binding Color, Converter={StaticResource OxycolorToColorConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</CheckBox.Style>
</CheckBox>
<Label Grid.Row="0" Grid.Column="1" Content="{Binding Title}" Padding="1" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
转换器:
[ValueConversion(typeof(OxyColor), typeof(Brush))]
class OxycolorToColor: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var ox = (OxyColor) value;
var color = Color.FromArgb(ox.A, ox.R, ox.G, ox.B);
var brush = new SolidColorBrush(color);
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return OxyColors.Purple;
}
}
您定义了一个 ControlTemplate 但在其中您没有使用背景 属性 因此它从未显示。
<ControlTemplate TargetType="{x:Type CheckBox}">
<Border Width="15"
Height="15"
BorderBrush="Gray"
BorderThickness="2"
CornerRadius="3"
Background="{TemplateBinding Background}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="{Binding Color, Converter={StaticResource OxycolorToColorConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>