如何在保留选择样式的同时绑定CheckBox的ListBox?

时间:2012-10-10 19:59:59

标签: c# wpf xaml

我有以下XAML将数据绑定到填充了CheckBoxes的ListBox:

        <ListBox Background="Transparent" Grid.Column="8" Grid.Row="3" ItemsSource="{Binding Path=StakeTypes}" Foreground="White"
        Name="lbStakes" ItemContainerStyle="{StaticResource SelectionListBoxItem}" SelectionChanged="lbStakes_SelectionChanged" SelectionMode="Extended">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBoxItem IsSelected="{Binding IsSelected, Mode=TwoWay}" HorizontalContentAlignment="Stretch">
                    <CheckBox Cursor="Hand" IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Text}" HorizontalContentAlignment="Stretch" />
                </ListBoxItem>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

问题是,ListBox选择的样式与手动创建每个ListBox项目的样式不同:

    <ListBox Background="Transparent" Cursor="Hand" Grid.Column="6" Grid.Row="3" Name="lbStakes" SelectionMode="Extended">
        <ListBoxItem IsSelected="True">
            <CheckBox IsChecked="True" Foreground="White" Content="Low" />
        </ListBoxItem>
        <ListBoxItem IsSelected="True">
            <CheckBox IsChecked="True" Foreground="White" Content="Medium" />
        </ListBoxItem>
        <ListBoxItem IsSelected="True">
            <CheckBox IsChecked="True" Foreground="White" Content="High" />
        </ListBoxItem>
        <ListBoxItem IsSelected="True">
            <CheckBox IsChecked="True" Foreground="White" Content="Highest" />
        </ListBoxItem>
    </ListBox>

以下是图片:

With Binding

Without Binding

我希望它看起来像第二张图片。非常感谢任何想法。

更新:以下是我尝试应用于ListBoxItem的样式:

    <Style x:Key="SelectionListBoxItem" TargetType="ListBoxItem">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border Name="Border" Padding="3" SnapsToDevicePixels="true">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource myBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2 个答案:

答案 0 :(得分:1)

两种样式不同,因为在绑定的ListBox中使用ItemContainerStyle="{StaticResource SelectionListBoxItem}",而在第二个snippit中,默认的列表框项样式适用。尝试从绑定列表框中删除此样式分配。

答案 1 :(得分:0)

<Window.Resources>
    <Style x:Key="SelectionListBoxItem" TargetType="ListBoxItem">
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border" BorderBrush="Black" BorderThickness="0.5" SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <ListBox Background="Transparent" Grid.Column="8" Grid.Row="3" ItemsSource="{Binding Path=IsSelectede}" Foreground="White"
    Name="lbStakes" ItemContainerStyle="{StaticResource SelectionListBoxItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBoxItem IsSelected="{Binding IsSelected, Mode=TwoWay}" HorizontalContentAlignment="Stretch" KeyboardNavigation.TabNavigation="None"> 
                    <CheckBox Cursor="Hand" IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Text}" HorizontalContentAlignment="Stretch" />
                </ListBoxItem>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

某些绑定属性已更改,请根据您的属性更正它们。还有刷子的样式。我希望这会有所帮助。