我重写了ListBoxItems&的默认模板。列表框。这里是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>
这里是CodeWehind 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]);
}
}
}
我想要实现的主要思想是创建可检查的ListBox。我希望拥有的一项功能是Select All
CheckBox
。但这里的问题。 SelectAll CheckBox
未放置在ListBox
内。所以它在它之下。
我希望焦点像SelectAll CheckBox
一样放在ListBox
内。这意味着,当我点击标签键时,焦点应该转到下一个CheckBox
。在我的例子中,如果ListBox有多个项目,它只是改变两个CheckBox
之间的焦点 - 在SelectAll CheckBox和第一个ListBox
项之间。但我希望它归结为下一个ListBox
项目。我尝试使用FocusManager's
属性,但没有任何反应。
有什么建议......?
答案 0 :(得分:0)
使用ItemsControl
代替ListBox
....来自ListBox
基类的Selector
来自此Control + Up / Down Arrow
,这个类以某种方式覆盖基于Tab的简单焦点,其项目为{{{ 1}}在ItemsPanel
。
当我使用ItemsControl
时,它有效。