C# 如何在保留选择样式的同时绑定复选框列表框?
我使用以下XAML将数据绑定到充满复选框的列表框:C# 如何在保留选择样式的同时绑定复选框列表框?,c#,wpf,xaml,C#,Wpf,Xaml,我使用以下XAML将数据绑定到充满复选框的列表框: <ListBox Background="Transparent" Grid.Column="8" Grid.Row="3" ItemsSource="{Binding Path=StakeTypes}" Foreground="White" Name="lbStakes" ItemContainerStyle="{StaticResource SelectionListBoxItem}" Selectio
<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 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>
以下是图片:
我希望它看起来像第二张图片。非常感谢您的任何想法
更新:以下是我尝试应用于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>
这两种样式不同,因为在绑定的列表框中,您使用的是
ItemContainerStyle=“{StaticResource SelectionListBoxItem}”
,而在第二个snipbit中,默认的列表框项目样式适用。尝试从绑定的列表框中删除此样式分配。这两种样式不同,因为在绑定的列表框中您使用的是ItemContainerStyle=“{StaticResource SelectionListBoxItem}”
,而在第二个snipbit中,默认的列表框项目样式适用。尝试从绑定列表框中删除此样式分配。
<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>
某些绑定属性已更改。请根据您的属性以及画笔的样式进行更正。我希望这会有所帮助。
某些绑定属性已更改。请根据您的属性以及画笔的样式进行更正。我希望这会有所帮助。我不确定我是否理解这个问题。。。您是否在询问如何设置
SelectedItem
(如果是,是否可以包含SelectionListBoxItem
?)的XAML?如何将您的ListBoxItem.IsSelected
与复选框.IsChecked进行同步?或者,当用户单击列表框项目
中的控件时,如何确保选中该项目,例如在复选框
上?我不确定我是否理解这个问题。。。您是否在询问如何设置SelectedItem
(如果是,是否可以包含SelectionListBoxItem
?)的XAML?如何将您的ListBoxItem.IsSelected
与复选框.IsChecked进行同步?或者,当用户单击列表框项目
中的控件(例如复选框
)时,如何确保选中该项目?这不起作用。对于-1很抱歉,我将其设置为+1,但希望它返回到0。好的,没有问题:)-那么,什么不起作用?删除ItemContainerStyle没有什么区别,还是其他什么地方出了问题?它根本不会影响样式。这不起作用。对于-1很抱歉,我将其设置为+1,但希望它返回到0。好的,没有问题:)-那么,什么不起作用?删除ItemContainerStyle没有什么区别,还是出了其他问题?它根本不会影响样式。