【问题标题】:Binding to Color property of Item in ListView doesn't work绑定到 ListView 中 Item 的 Color 属性不起作用
【发布时间】:2016-05-04 15:17:03
【问题描述】:

我有这个代码。 ListView 中的每个项目都包含 Label 和 CheckBox 元素。 Label 表示绑定的 LineSeries 项的标题,Checkbox 绑定到 LineSeries 项的 IsVisible 属性。

当 LineSeries 元素可见时,我希望触发器将 Background 属性(复选框 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;
    }
}

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    您定义了一个 ControlTemplate,但在其中您不使用 Background 属性,因此它永远不会显示。

    <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>
    

    【讨论】:

    • @FonyFazoulyanov 使用 Style 并将 Command 属性设置为绑定
    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 2020-11-26
    • 2015-06-15
    • 2011-01-18
    • 2018-02-20
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多