列表框总是自动选择第一项

发布于 2024-08-04 10:16:17 字数 92 浏览 9 评论 0原文

ListBox 的行为是自动选择第一项,我怎样才能避免这种情况?

注意:我更喜欢使用纯 xaml 来完成此操作,如果您有任何代码隐藏想法,请不要打扰自己。

ListBox's behavior is that the first item is selected automatically, how can I avoid that??

Note: I prefer to do this with pure xaml, if you have any code-behind ideas then please don't bother yourself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一片旧的回忆 2024-08-11 10:16:17

尝试

IsSynchronizedWithCurrentItem="False"

Try

IsSynchronizedWithCurrentItem="False"

素罗衫 2024-08-11 10:16:17

好吧,我尝试使用 FocusManager.FocusedElement .. 并将初始焦点设置为
列表框本身..因此它具有焦点..但没有选择任何元素..
如果您按下或按 Tab 键..将选择列表框的第一个元素...

<Window
  ...... 
  FocusManager.FocusedElement="{Binding ElementName=listbox2}">
    <ListBox x:Name="listbox2" HorizontalAlignment="Left"
        VerticalAlignment="Bottom" Width="117.333" Height="116" 
        Margin="30.667,0,0,30">
        <ListBoxItem>Jim</ListBoxItem>
        <ListBoxItem>Mark</ListBoxItem>
        <ListBoxItem>Mandy</ListBoxItem>
</ListBox>

Well i tried this using FocusManager.FocusedElement .. and made the intial focus to
listbox itself.. so it has the focus..but no element is selected..
if u press down or tab ..the 1st element of the listbox will be selected...

<Window
  ...... 
  FocusManager.FocusedElement="{Binding ElementName=listbox2}">
    <ListBox x:Name="listbox2" HorizontalAlignment="Left"
        VerticalAlignment="Bottom" Width="117.333" Height="116" 
        Margin="30.667,0,0,30">
        <ListBoxItem>Jim</ListBoxItem>
        <ListBoxItem>Mark</ListBoxItem>
        <ListBoxItem>Mandy</ListBoxItem>
</ListBox>
忆依然 2024-08-11 10:16:17

删除 IsSynchronizedWithCurrentItem="True" 并根据需要将其添加到下一个 SelectionChanged 事件中。
这解决了我的问题

remove IsSynchronizedWithCurrentItem="True" an add it with the next SelectionChanged event if needed.
This solved my problem

东北女汉子 2024-08-11 10:16:17

您可以将 SelectedIndex 设置为 -1 :

<ListBox ItemsSource="{Binding MyData}" SelectedIndex="-1"/>

注意:我想用纯 xaml 来完成此操作,如果您有任何代码隐藏想法,请不要打扰自己。

不幸的是,您无法在 XAML 中完成所有操作...您通常可以避免代码隐藏,但您仍然需要编写转换器、标记扩展或附加属性

You could set SelectedIndex to -1 :

<ListBox ItemsSource="{Binding MyData}" SelectedIndex="-1"/>

Note: I want to do this with pure xaml, if you have any code-behind ideas then please don't bother yourself.

Unfortunately you can't do everything in XAML... you can usually avoid code-behind, but you still need to write converters, markup extensions or attached properties

和我恋爱吧 2024-08-11 10:16:17

这是我经常使用的一种技巧。它建立在上面将 FocusedElement 属性添加到 WindowUserControl 的示例之上。

我的协议是,我不希望窗口上的任何控件获得焦点。对我来说,解决方案是创建一个没有 UI 的虚拟控件并将焦点分配给它。碰巧 Control 完全符合要求:

<UserControl
    x:Class="MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FocusManager.FocusedElement="{Binding ElementName=focus_thief}"
    mc:Ignorable="d">
    <Grid>
        <!-- no renderable UI -->
        <Control Name="focus_thief"/>
        <!-- wants focus, but won't get it -->
        <ListBox>
            <ListBoxItem>First Item</ListBoxItem>
        </ListBox>
    </Grid>
</UserControl>

Here's a technique I use quite often. It builds on the above example of adding the FocusedElement attribute to your Window or UserControl.

My deal is that I don't want ANY of the controls on my window to have focus. The solution for me is to create a dummy control that has no UI and assign focus to that. It just so happens that Control fits the bill perfectly:

<UserControl
    x:Class="MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FocusManager.FocusedElement="{Binding ElementName=focus_thief}"
    mc:Ignorable="d">
    <Grid>
        <!-- no renderable UI -->
        <Control Name="focus_thief"/>
        <!-- wants focus, but won't get it -->
        <ListBox>
            <ListBoxItem>First Item</ListBoxItem>
        </ListBox>
    </Grid>
</UserControl>
陪你到最终 2024-08-11 10:16:17
<ListBox SelectioMode="Single" SelectedIndex="-1"/>
<ListBox SelectioMode="Single" SelectedIndex="-1"/>
小糖芽 2024-08-11 10:16:17

SelectedIndex 是您正在寻找的属性吗?或者也许我没明白你的意思...

Is SelectedIndex the property you're looking for ? Or maybe I don't get your point...

终弃我 2024-08-11 10:16:17

这里同样的问题。有人找到“干净”的解决方案吗?
这里的问题是一样的,它会导致一堆触发器执行。

明显的解决方案/修复:

  1. 从 XAML 中删除 SelectionChanged 事件处理程序
  2. 在 InitializeComponents 加载列表框后在构造函数中添加处理程序。

Same issue here. Anyone found a "clean" solution?
The problem is the same here, it causes a bunch of triggers to execute.

Obvious solution/fix:

  1. Remove SelectionChanged event handlers from XAML
  2. Add handlers in the constructor after InitializeComponents has loaded the listbox.
小嗷兮 2024-08-11 10:16:17

添加一个空白项目。

Add a blank item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文