2011-09-26 154 views
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> 

這裏隱藏代碼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 AllCheckBox。但這裏的問題。 SelectAll CheckBox未鋪設在ListBox內。所以它在它之下。

我想讓焦點像SelectAll一樣工作CheckBox躺在ListBox內。這意味着,當我點擊標籤鍵,重點應轉到下一個CheckBox。在我的情況下,如果ListBox有超過1個項目,它只是將焦點在兩個CheckBox es之間 - 在SelectAll CheckBox和第一個ListBox項目之間。但我希望它下降到下一個ListBox項目。我嘗試玩FocusManager's屬性,但沒有任何反應。 任何建議...?

Bad focus example

回答

0

使用ItemsControl而不是ListBox ....從Selector基類,這個類的ListBox冰雹莫名其妙覆蓋簡單的標籤基於重點及其項目與Control + Up/Down Arrow在其ItemsPanel

當我使用ItemsControl它的工作。

+0

我會嘗試,但我用ListBox,因爲它有SelectedItems屬性。我認爲這將是很多代碼來重現這種行爲。謝謝 – stukselbax