這裏是發生了什麼:防止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;
其中fsItems
是ObservableCollection<OuterItem>
。
我描述的可用性問題發生時,我Clear()
收集(fsItems
)並填寫新的項目。
虛擬化可能是一個問題 – 2009-10-21 12:41:07