Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到ListView中项的颜色属性不';行不通_C#_Wpf_Xaml_Binding - Fatal编程技术网

C# 绑定到ListView中项的颜色属性不';行不通

C# 绑定到ListView中项的颜色属性不';行不通,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我有这个密码。ListView中的每个项目都包括标签和复选框元素。标签表示绑定的Lineseries项目的标题,复选框绑定到Lineseries项目的IsVisible属性 我希望当LineSeries元素可见时,触发器将背景属性(Checkbox ControlTemplate中的Border元素)设置为LineSeries元素的颜色。设置为浅灰色有效,同时更改IsVisible属性也有效,但边框的背景不会更改 <ListView Name="MyListView" Horizon

我有这个密码。ListView中的每个项目都包括标签和复选框元素。标签表示绑定的Lineseries项目的标题,复选框绑定到Lineseries项目的IsVisible属性

我希望当LineSeries元素可见时,触发器将背景属性(Checkbox ControlTemplate中的Border元素)设置为LineSeries元素的颜色。设置为浅灰色有效,同时更改IsVisible属性也有效,但边框的背景不会更改

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

您定义了一个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>

您定义了一个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使用样式并将命令属性设置为binding@FonyFazoulyanov使用样式并将命令属性设置为绑定