【问题标题】:how to avoid first item selection by default in wpf Listbox?如何避免默认情况下在wpf Listbox中选择第一项?
【发布时间】:2013-01-10 12:26:09
【问题描述】:

我想避免在应用程序加载时默认在Listbox 中发生的第一项选择。我试过了

SelectedIndex= -1

但它不起作用。我不希望在加载ListBox 时选择任何项目,直到用户从Listbox 中选择任何项目。 我将IsSynchronization 设置为true,但设置为false 也不能解决我的问题。尽管我必须始终将IsSychronization 设置为true。但没关系。我做了谷歌,但找不到任何相关的答案。

谁能告诉我怎么做?

【问题讨论】:

  • 我试过这样,它对我有用...<ListBox Canvas.Left="327" Canvas.Top="145" Height="100" Name="lstBx1" Width="120" SelectedIndex="-1" /> 当我将索引更改为 0 或 1 时,它显示为选中..
  • 默认不选择任何项目。 IsSychronization 是什么?
  • 抱歉,这是 Listbox 中的 IsSynchronizedWithCurrentItem 属性。
  • 我做了 SelectedIndex=-1 但它不起作用。你确定它适合你吗

标签: wpf xaml listbox


【解决方案1】:

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

当我切换到使用CollectionViewSource 作为ListBox.ItemsSource 时,突然默认选择了第一项。

原来这是由于ListBox.IsSynchronizedWithCurrentItem。将其设置为

IsSynchronizedWithCurrentItem="False"

ListBox 上恢复了默认情况下没有选择任何内容的期望行为。

【讨论】:

    【解决方案2】:

    将 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>
    

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2016-06-13
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多