c# - 列表框选择模式在 WPF 中不起作用

标签 c# wpf xaml listbox

我有一个包含一些项目的 ListBox。我想实现:

1) CTRL键选择不同的项目。

2) Tab 键改变项目。

3) Shift + tab 控件..

  • 我正在使用 SelectionMode=Extended,但是 CTRL 不起作用,为什么?

  • 当我按下 Tab 键时,它只在第一个项目上切换,为什么?

Xaml:

 <Window.Resources>
    <Style x:Key="ButtonStyle2" TargetType="{x:Type CheckBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <Canvas>
                        <Path x:Name="path" Data="{Binding PathCoords}" Margin="{Binding Margin}" Fill="#FFF4F4F5" Stretch="Fill" Stroke="Black"/>
                    </Canvas>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Stroke" TargetName="path" Value="#FFCF2222"/>
                            <Setter Property="StrokeThickness" TargetName="path" Value="2"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox ItemsSource="{Binding Items}" SelectionMode="Extended">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas></Canvas>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>   
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Style="{DynamicResource ButtonStyle2}" Focusable="True"></CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

代码:

namespace WpfApplication13
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = new Data();
        InitializeComponent();
    }
}
public class Element
{
    public string PathCoords { get; set; }
    public string Margin { get; set; }
    public int id { get; set; }
}
public class Data
{
    public List<Element> Items { get; set; }
    public Data()
    {
        Items = new List<Element>();
        Items.Add(new Element { PathCoords = "M52,92L89.5,57.5 99.5,100.5z" });
        Items.Add(new Element { PathCoords = "M131,104L150.5,39.5 204.5,87.5 155.5,88.5z" ,Margin="131,39.5,0,0"});
        Items.Add(new Element { PathCoords = "M232,105L231.5,49.5 294.5,49.5 291.5,97.5z", Margin = "231.5,49.5,0,0" });
        Items.Add(new Element { PathCoords = "M75,183L85.5,154.5 107.5,185.5z", Margin = "75,154.5,0,0" });
        Items.Add(new Element { PathCoords = "M167,222L166.5,169.5 230.5,190.5z", Margin = "166.5,169.5,0,0" });
        Items.Add(new Element { PathCoords = "M258,199L273.5,146.5 332.5,161.5 327.5,207.5z", Margin = "258,146.5,0,0" });
    }
}
}

最佳答案

要使 SelectionMode=Extended 正常工作,您的 ListBox 中必须有一个 Trigger 来通知 CheckBox 已选择新的 ListBoxItem

目前您可以选择多个项目,但是只有其中一个项目中的 CheckBox 会获得焦点,因此只有一个 Path 的颜色会改变。

因此,如果您将 CheckBox 的触发器中的条件设置为 IsChecked 而不是 IsFocused,如下所示:

<Style x:Key="ButtonStyle2" TargetType="{x:Type CheckBox}">
     ...
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Stroke" TargetName="path" Value="#FFCF2222"/>
            <Setter Property="StrokeThickness" TargetName="path" Value="2"/>
        </Trigger>
     ...
</Style>

然后为您的 ListBox.ItemTemplate 添加适当的 DataTrigger,如下所示:

</ListBox>
    ...
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Name="chb" Style="{DynamicResource ButtonStyle2}" Focusable="True"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType=
                                    {x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                    <Setter Property="IsChecked" TargetName="chb" Value="True"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

那么多选就可以了。

关于c# - 列表框选择模式在 WPF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023767/

相关文章:

c# - 将文本保存到文件

c# - 在不消耗内存的情况下运行连续的 Windows 后台任务

c# - 有人可以解释 URI 类重载版本中的 RelativeOrAbsolute 参数吗?

c# - Prism MVVM : 'No parameterless constructor defined for type' Error when binding ViewModel to View

WPF 控件模板与用户控件

c# - 在MVVM中处理MouseEnter over命令

c# - OnPaint 中的 Winforms Frameratelock?

Wpf 组合框圆角

c# - 自动适应列表框子项的宽度

wpf 简单 mvvm 绑定(bind)不工作