将ControlTemplate触发器应用于ListView中的CheckBox

时间:2014-11-17 22:17:29

标签: wpf xaml

我试图在ListView中覆盖CheckBox的外观。 CheckBox绑定到可以为空的bool(bool?)。

这种风格似乎根本没有应用。我在这里做错了什么?

<ListView Grid.Row="1" Margin="10" VerticalAlignment="Top" ItemsSource="{Binding Prerequisites}" d:DataContext="{d:DesignInstance Type=viewModels:MockPrerequisiteViewModel, IsDesignTimeCreatable=True}">
    <ListView.Resources>
        <Style x:Key="StyleCustomCheckBox" TargetType="{x:Type CheckBox}">
            <Setter Property="FontSize" Value="14" />
            <Setter Property="Margin" Value="10,0,0,0" />
            <Setter Property="VerticalAlignment" Value="Center" />                            
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <StackPanel Orientation="Horizontal">
                            <Path x:Name="MyIcon" Width="18" Height="18" Stretch="Fill" Fill="#FF00FF00"
                          Data="M-150.204,626.126C-152.317,626.126 -154.429,626.126 -156.541,626.126 -167.642,633.42 -180.629,646.047 -189.668,657.238 -190.916,658.782 -192.945,662.362 -193.701,662.422 -194.041,662.448 -198.024,659.719 -198.614,659.297 -202.818,656.279 -205.779,653.709 -209.257,650.899 -211.248,652.172 -212.879,653.805 -214.153,655.797 -206.627,665.074 -200.283,675.534 -193.124,685.18 -181.491,665.11 -168.473,644.683 -152.796,629.006 -151.735,627.946 -149.817,626.933 -150.204,626.126z"/>
                            <ContentPresenter VerticalAlignment="Center" Margin="10,0,0,0" />
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter TargetName="MyIcon" Property="Data" Value="F1M-1774.05,-6263.81L-1787.51,-6277.27 -1773.22,-6291.56C-1769.23,-6295.55 -1769.24,-6302.03 -1773.21,-6306.01 -1777.19,-6309.98 -1783.67,-6309.99 -1787.66,-6305.99L-1801.95,-6291.71 -1816.79,-6306.55C-1820.79,-6310.55 -1827.26,-6310.54 -1831.24,-6306.55 -1835.22,-6302.58 -1835.23,-6296.11 -1831.24,-6292.12L-1816.39,-6277.27 -1830.4,-6263.25C-1834.4,-6259.26 -1834.38,-6252.8 -1830.4,-6248.82 -1826.42,-6244.84 -1819.96,-6244.82 -1815.96,-6248.82L-1801.95,-6262.83 -1788.49,-6249.37C-1784.5,-6245.38 -1778.03,-6245.39 -1774.06,-6249.37 -1770.07,-6253.35 -1770.06,-6259.82 -1774.05,-6263.81" />
                                <Setter TargetName="MyIcon" Property="Fill" Value="#FFFF0000" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>                            
            <GridViewColumn Width="Auto" Header="Machine" DisplayMemberBinding="{Binding Context.Hostname}"/>
            <GridViewColumn Width="Auto" Header="Prerequisite" DisplayMemberBinding="{Binding Label}"/>
            <GridViewColumn Width="Auto" Header="Status" DisplayMemberBinding="{Binding Status}"/>
            <GridViewColumn Width="Auto" Header="Result" DisplayMemberBinding="{Binding Result}">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Result}" IsThreeState="True" IsEnabled="False" Style="{StaticResource StyleCustomCheckBox}"/>                                            
                    </DataTemplate>
                </GridViewColumn.CellTemplate>                                
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

1 个答案:

答案 0 :(得分:1)

您需要从DisplayMemberBinding="{Binding Result}"删除GridViewColumn

当您将复选框绑定到结果时,当您设置DisplayMemberBinding属性时,GridView会自动使用字符串表示。

       <GridViewColumn Width="Auto" Header="Result">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Result}" IsThreeState="True" IsEnabled="False" Style="{StaticResource StyleCustomCheckBox}"/>                                            
                </DataTemplate>
            </GridViewColumn.CellTemplate>                                
        </GridViewColumn>