2009-10-20 90 views
1

這裏是發生了什麼:防止ListBox聚焦,但保留ListBoxItem(s)可聚焦

我有一個列表框項目。列表框有重點。某些項目(例如第5個)被選中(具有藍色背景),但沒有「邊框」。

當我按'向下'鍵時,焦點從列表框移動到第一個ListBoxItem。
(我想要的是使第6項選擇,無論'邊框')

當我使用'標籤'導航,列表框再也沒有收到焦點。

但是,當收集被清空並再次填充時,ListBox本身獲得焦點,按'向下'將焦點移動到該項目。

如何防止ListBox獲得焦點?

P.S.
listBox1.SelectedItem是我自己的班級,我不知道如何製作ListBoxItem.Focus()吧。

EDIT:代碼

的Xaml

<UserControl.Resources> 
    <me:BooleanToVisibilityConverter x:Key="visibilityConverter"/> 
    <me:BooleanToItalicsConverter x:Key="italicsConverter"/> 
</UserControl.Resources> 
<ListBox x:Name="lbItems"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <ProgressBar HorizontalAlignment="Stretch" 
          VerticalAlignment="Bottom" 
          Visibility="{Binding Path=ShowProgress, Converter={StaticResource visibilityConverter}}" 
          Maximum="1" 
          Margin="4,0,0,0" 
          Value="{Binding Progress}" 
          /> 
       <TextBlock Text="{Binding Path=VisualName}" 
          FontStyle="{Binding Path=IsFinished, Converter={StaticResource italicsConverter}}" 
          Margin="4" 
          /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <me:OuterItem Name="Regular Folder" IsFinished="True" Exists="True" IsFolder="True"/> 
    <me:OuterItem Name="Regular Item" IsFinished="True" Exists="True"/> 
    <me:OuterItem Name="Yet to be created" IsFinished="False" Exists="False"/> 
    <me:OuterItem Name="Just created" IsFinished="False" Exists="True"/> 
    <me:OuterItem Name="In progress" IsFinished="False" Exists="True" Progress="0.7"/> 
</ListBox> 

其中OuterItem是:

public class OuterItem : IOuterItem 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    public bool IsFolder { get; set; } 

    public bool IsFinished { get; set; } 
    public bool Exists { get; set; } 
    public double Progress { get; set; } 

    /// Code below is of lesser importance, but anyway 
    /// 
    #region Visualization helper properties 
    public bool ShowProgress 
    { 
     get 
     { 
      return !IsFinished && Exists; 
     } 
    } 
    public string VisualName 
    { 
     get 
     { 
      return IsFolder ? "[ " + Name + " ]" : Name; 
     } 
    } 
    #endregion 

    public override string ToString() 
    { 
     if (IsFinished) 
      return Name; 

     if (!Exists) 
      return " ??? " + Name; 

     return Progress.ToString("0.000 ") + Name; 
    } 

    public static OuterItem Get(IOuterItem item) 
    { 
     return new OuterItem() 
     { 
      Id = item.Id, 
      Name = item.Name, 
      IsFolder = item.IsFolder, 

      IsFinished = item.IsFinished, 
      Exists = item.Exists, 
      Progress = item.Progress 
     }; 
    } 
} 

Сonverters是:

/// Are of lesser importance too (for understanding), but will be useful if you copy-paste to get it working 
public class BooleanToItalicsConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool normal = (bool)value; 
     return normal ? FontStyles.Normal : FontStyles.Italic; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class BooleanToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool exists = (bool)value; 
     return exists ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

但最重要的是,UserControl.Loaded()有:

lbItems.Items.Clear(); 
lbItems.ItemsSource = fsItems; 

其中fsItemsObservableCollection<OuterItem>

我描述的可用性問題發生時,我Clear()收集(fsItems)並填寫新的項目。

+0

虛擬化可能是一個問題 – 2009-10-21 12:41:07

回答

1

請提供您的代碼。通常這個問題的原因在於ContentPresenters和KeyboardNavigation.IsTabStop屬性。但有時候並非如此。所以代碼會有所幫助。

+0

是的,請提供一個片段和所需結果的描述,這是很難解密的問題.... – 2009-10-23 18:21:51

0

您的問題的答案可能取決於您的列表框越來越關注的方式。如果您使用訪問密鑰(例如:alt + c),則這是解決方案。您必須實現自己的列表框控件並重寫OnAccessKey方法。如果這不是你的場景,那麼我會建議看看OnIsKeyboardFocusWithinChanged方法。嘗試使用我在下面的代碼中使用的相同方法。

protected override void OnAccessKey(System.Windows.Input.AccessKeyEventArgs e) 
    { 
     if (SelectedIndex >= 0) 
     { 
      UIElement element = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as UIElement; 
      if (element != null) 
      { 
       element.Focus(); 
      } 
     }   
    }