我正在重写 ListBoxItems 和 ListBox 的默认模板。这里 MainWindow.xaml
<Window x:Class="NestedBindingTry.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Window_Loaded"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<Style TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<StackPanel
IsItemsHost="True"
FocusManager.IsFocusScope="False"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel FocusManager.IsFocusScope="True"
KeyboardNavigation.ControlTabNavigation="Cycle"
KeyboardNavigation.DirectionalNavigation="Cycle">
<StackPanel>
<CheckBox Content="SelectAll" />
</StackPanel>
<ListBox SelectionMode="Multiple" Name="M" ItemsSource="{Binding}" FocusManager.IsFocusScope="False">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CheckBox"
Storyboard.TargetProperty="IsChecked">
<DiscreteBooleanKeyFrame KeyTime="0"
Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CheckBox"
Storyboard.TargetProperty="IsChecked">
<DiscreteBooleanKeyFrame KeyTime="0"
Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CheckBox"
Storyboard.TargetProperty="IsChecked">
<DiscreteBooleanKeyFrame KeyTime="0"
Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<CheckBox Focusable="False"
Name="CheckBox"
Content="{Binding}"
Checked="CheckBox_Checked"
Unchecked="CheckBox_Unchecked"
VerticalAlignment="Center"
Margin="0,0,3,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
</StackPanel>
</Grid>
</Window>
这里 codeBehind MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NestedBindingTry
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new List<string>() { "123", "546", "789"};
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
CheckBox bik = sender as CheckBox;
DependencyObject dob = bik as DependencyObject;
while (dob.GetType() != typeof(ListBox))
{
dob = VisualTreeHelper.GetParent(dob);
}
ListBox my = dob as ListBox;
if (!my.SelectedItems.Contains(bik.DataContext))
my.SelectedItems.Add(bik.DataContext);
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
CheckBox bik = sender as CheckBox;
DependencyObject dob = bik as DependencyObject;
while (dob.GetType() != typeof(ListBox))
{
dob = VisualTreeHelper.GetParent(dob);
}
ListBox my = dob as ListBox;
if (my.SelectedItems.Contains(bik.DataContext))
my.SelectedItems.Remove(bik.DataContext);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.M.SelectedItems.Add(((IList<string>)this.DataContext)[0]);
this.M.SelectedItems.Add(((IList<string>)this.DataContext)[2]);
}
}
}
我想要实现的主要想法是创建可检查的列表框。我想要拥有的一个功能 - 是Select All
CheckBox
. 但这里的问题。SelectAllCheckBox
不在里面ListBox
。所以它在它之下。
我希望这个焦点像 SelectAll 一样工作CheckBox
在里面ListBox
。这意味着,当我单击Tab键时,焦点应该转到下一个CheckBox
. 在我的情况下,如果 ListBox 有超过 1 个项目,它只是在两个CheckBox
es 之间更改焦点 - 在 SelectAll CheckBox 和第一个ListBox
项目之间。但我希望它归结为下一个ListBox
项目。我尝试使用FocusManager's
属性,但没有任何反应。有什么建议...?