Wpf 防止ListBox聚焦,但保持ListBoxItem可聚焦

Wpf 防止ListBox聚焦,但保持ListBoxItem可聚焦,wpf,listbox,focus,Wpf,Listbox,Focus,发生的情况如下: 我有一个包含项目的列表框。列表框有焦点。某些项目(例如,第5项)被选中(有蓝色背景),但没有“边框” 当我按下“向下”键时,焦点从ListBox移动到第一个ListBoxItem。 (我要做的是选择第6项,而不考虑“边框”) 当我使用“Tab”导航时,列表框再也不会收到焦点 但当集合再次清空和填充时,ListBox本身会获得焦点,按下“向下”键会将焦点移动到该项 如何防止ListBox获得关注 附加说明 listBox1.SelectedItem是我自己的类,我不知道如何将Li

发生的情况如下:

我有一个包含项目的列表框。列表框有焦点。某些项目(例如,第5项)被选中(有蓝色背景),但没有“边框”

当我按下“向下”键时,焦点从ListBox移动到第一个ListBoxItem。
(我要做的是选择第6项,而不考虑“边框”)

当我使用“Tab”导航时,列表框再也不会收到焦点

但当集合再次清空和填充时,ListBox本身会获得焦点,按下“向下”键会将焦点移动到该项

如何防止ListBox获得关注

附加说明
listBox1.SelectedItem
是我自己的类,我不知道如何将
ListBoxItem
从它变成
.Focus()

编辑:代码

Xaml:

С转换器为:

/// 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.load()
具有:

lbItems.Items.Clear();
lbItems.ItemsSource = fsItems;
其中
fsItems
observedcollection


我描述的可用性问题发生在我清除该集合(
fsItems
)并填充新项目时。

请提供您的代码。通常,此问题的原因在于ContentPresenters和KeyboardNavigation.IsTabStop属性。但有时并非如此。因此,代码会有所帮助。

请提供您的代码。通常,此问题的原因在于ContentPresenters和KeyboardNavigation.IsTabStop属性。但有时并非如此。因此,代码会有所帮助。

问题的答案可能取决于列表框获得焦点的方式。如果您使用的是访问键(例如:alt+c),那么下面是解决方案。您必须实现自己的listbox控件并重写OnAccessKey方法。如果这不是您的场景,那么我建议您研究oniskeyboardfocushinchanged方法。尝试使用我在下面代码中使用的相同方法

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

问题的答案可能取决于列表框获得焦点的方式。如果您使用的是访问键(例如:alt+c),那么下面是解决方案。您必须实现自己的listbox控件并重写OnAccessKey方法。如果这不是您的场景,那么我建议您研究oniskeyboardfocushinchanged方法。尝试使用我在下面代码中使用的相同方法

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

虚拟化可能是一个问题虚拟化可能是一个问题,请提供一个片段和所需结果的描述,很难对问题进行解密…是的,请提供一个片段和所需结果的描述,很难对问题进行解密。。。。
protected override void OnAccessKey(System.Windows.Input.AccessKeyEventArgs e)
    {
        if (SelectedIndex >= 0)
        {
            UIElement element = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as UIElement;
            if (element != null)
            {
                element.Focus();
            }
        }           
    }