如何避免在wpf列表框中默认选择第一个项目?

Vje*_*orh 7 wpf xaml listbox

我想避免在Listbox应用程序加载时默认情况下首先选择项目.我试过了

SelectedIndex= -1
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我不希望在ListBox加载时选择任何项目,直到用户从中选择任何项目Listbox.我已经IsSynchronization设置为true但是它false也没有解决我的问题.虽然我必须保持IsSychronization设置为true始终.但那没关系.我做谷歌但没有找到任何相关的答案.

谁能告诉我怎么做?

小智 13

我发生了这件事.当我ListBox.ItemsSource直接绑定到视图模型中的集合时,默认情况下没有选择任何内容.

当我切换到使用a CollectionViewSource作为时ListBox.ItemsSource,突然第一项被默认选中.

事实证明这是由于ListBox.IsSynchronizedWithCurrentItem.设置为

IsSynchronizedWithCurrentItem="False"
Run Code Online (Sandbox Code Playgroud)

ListBox具有任何缺省选中的恢复所需的行为.


Ωme*_*Man 0

将 IsEnabled 绑定到一个布尔值,该布尔值将在用户可以选择时进行标记。列表框仍然可见,但不可选择。这是我使用的标签的样式代码,必须在登录期间启用它,但在操作停止后就不能启用。

  <Style x:Key="LabelStyle" TargetType="{x:Type Label}">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Width" Value="70" />
        <Setter Property="Height" Value="28"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=LoginInProcess}" Value="True">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=LoginInProcess}" Value="False">
                <Setter Property="IsEnabled" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
Run Code Online (Sandbox Code Playgroud)