Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在列表框中选择项目时,将焦点放在另一个控件上_Wpf_Focus_Listboxitem - Fatal编程技术网

Wpf 在列表框中选择项目时,将焦点放在另一个控件上

Wpf 在列表框中选择项目时,将焦点放在另一个控件上,wpf,focus,listboxitem,Wpf,Focus,Listboxitem,我有一个文本框,它应该始终处于焦点位置。 同时我还有一个列表框。 当用户单击此列表中的某个项目时,单击的项目将获得焦点 我尝试为我的ListBox中的每个ListBoxItem设置Focusable=“false”,但在这种情况下无法选择任何项目。 我使用dotPeek发现了以下代码: private void HandleMouseButtonDown(MouseButton mouseButton) { if (!Selector.UiGetIsSelectable((Dependenc

我有一个文本框,它应该始终处于焦点位置。 同时我还有一个列表框。 当用户单击此列表中的某个项目时,单击的项目将获得焦点

我尝试为我的ListBox中的每个ListBoxItem设置Focusable=“false”,但在这种情况下无法选择任何项目。 我使用dotPeek发现了以下代码:

private void HandleMouseButtonDown(MouseButton mouseButton)
{
  if (!Selector.UiGetIsSelectable((DependencyObject) this) || !this.Focus())
    return;
  ...
}

有什么办法解决我的问题吗?

您可以在ListBoxItems上处理PreviewMouseDown,并将事件标记为已处理,这将停止转移焦点

您可以设置
e.Handled=true
,因为MouseButtonEventArgs是一个

此演示用于保持对文本框的关注:

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
    <StackPanel FocusManager.FocusedElement="{Binding ElementName=textBox}">
        <TextBox  x:Name="textBox" />
        <ListBox x:Name="listBox">
            <ListBoxItem PreviewMouseDown="ListBoxItem_PreviewMouseDown">1</ListBoxItem>
            <ListBoxItem PreviewMouseDown="ListBoxItem_PreviewMouseDown">2</ListBoxItem>
            <ListBoxItem PreviewMouseDown="ListBoxItem_PreviewMouseDown">3</ListBoxItem>
        </ListBox>
    </StackPanel>
</Window>

1.
2.
3.
代码隐藏

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ListBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ListBoxItem;
            if (item == null) return;
            listBox.SelectedItem = item;
            e.Handled = true;
        }
    }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
private void ListBoxItem_PreviewMouseDown(对象发送器,鼠标按钮Ventargs e)
{
var item=发送方作为ListBoxItem;
如果(item==null)返回;
listBox.SelectedItem=项目;
e、 已处理=正确;
}
}
}

是否要选择listbox中的项目,但不以蓝色突出显示该项目?我希望能够通过鼠标选择listbox项目,但不会失去对我的文本框的关注。您可以在listbox的SelectionChanged事件中再次设置对文本框的关注。是的,这会起作用,但这是一个非常糟糕的解决方案。TextBox和ListBox位于不同的用户控件中,因此彼此都不了解。我更愿意实现我自己的listbox和ListBoxItem,而不是使用这样的方法。您必须按父/子关系查找该文本框。我不知道有没有可能。如果可能的话,我们可以在网上找到它。