1

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]);
    }
}
}

私が達成したい主なアイデアは、チェック可能なListBoxを作成することです。私が持ちたい機能の 1 つは、Select All CheckBox. しかしここで問題。SelectAllCheckBoxは内部に配置されていませんListBox。その下にあるので。

そのフォーカスが SelectAll CheckBoxlay insideのように機能することを望みListBoxます。つまり、Tabキーをクリックすると、フォーカスが次のCheckBox. 私の場合、ListBoxに複数のアイテムがある場合、CheckBoxSelectAll CheckBoxと最初のListBoxアイテムの間の2つのesの間でフォーカスを変更するだけです。しかし、私はそれが次の項目に行きたいと思っていListBoxます。プロパティで遊んでみましたFocusManager'sが、何も起こりません。なにか提案を...?

悪いフォーカスの例

4

1 に答える 1

0

... のItemsControl代わりに使用してください。基本クラスからの歓迎であり、このクラスは何らかの方法でその項目の単純なタブベースのフォーカスをその .xml でオーバーライドします。ListBoxListBoxSelectorControl + Up / Down ArrowItemsPanel

使っItemsControlてみたら効きました。

于 2011-09-26T12:44:13.483 に答える