如何确保没有项目在数据绑定列表框选择? [英] How to ensure that no item is selected in databound ListBox?
问题描述
确定,这一定是一个重复的问题,但我无法找到答案: 我有一个数据绑定到一个集合列表框。默认情况下项目[0]选择。我怎样才能prevent这是为了确保SelectionChanged事件引发任何时候我点击一个ListBoxItem的?
OK, this must be a duplicate question, but I cannot find the answer: I have a listbox that is databound to a Collection. By default Items[0] is selected. How can I prevent this in order to ensure that the SelectionChanged event is raised any time I click a ListBoxItem?
编辑: 我已经解散了的SelectedIndex = -1路线,但我又试了一次:在列表框的构造(或在XAML属性)不起作用的SelectedIndex设置为-1。看来,列表框的初始化后填充和all.Same的故事后的selectedIndex将成为0则selectedItem设置为null;
I had already dismissed the SelectedIndex=-1 route, but I tried again: Setting the SelectedIndex to -1 in the Listbox's constructor (or as an attribute in XAML) does not work. It seems that the listbox is populated after the initialization and the selectedindex will become 0 after all.Same story for setting the SelectedItem to null;
我尝试这样做:
<ListBox ItemsSource="{Binding Value}"
SelectionChanged="ListBox_SelectionChanged"
IsSynchronizedWithCurrentItem="True"
Loaded="ListBox_Loaded">
</ListBox>
与
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count==0)return;//Prevents stackoverflow! ;-)
((ListBox)e.OriginalSource).SelectedItem=null;
}
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
((ListBox) sender).SelectedItem = null;
}
这个作品,但它插入在该列表框显示,这是非常丑陋的物品顶空行.... 在我的具体情况,我可以通过删除IsSynchronizedWithCurrentItem属性解决问题。
This works, BUT it inserts a blank line on top of the items that the listbox displays, which is very ugly.... In my particular case I could solve the problem by removing the IsSynchronizedWithCurrentItem attribute.
但是,我能想到的许多场景中,这是无法接受的。的
上面的说法是无稽之谈:要么你想利用主从绑定,并设置IsSynchronizedWithCurrentItem为true,或者你没有。这是相当不可能的,你想利用主从绑定的,然后让当前没有选择在列表框中的项目所有时间
The above statement is nonsense: either you want to make use of master-detail binding and set the IsSynchronizedWithCurrentItem to true, or you don't. It is rather unlikely that you want to make use of master-detail binding and then have no currently selected item in your listbox all the time
推荐答案
使用的CollectionView作为一个的ItemsSource为您的列表框中。最初选择新鲜的WPF项目没有项目:
Use CollectionView as an ItemsSource for your list box. A fresh WPF project with no item initially selected:
XAML:
<Window x:Class="ListBoxDataBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ListBox
ItemsSource="{Binding Path=Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
SelectionChanged="ListBox_SelectionChanged"
IsSynchronizedWithCurrentItem="True"
>
</ListBox>
</Grid>
</Window>
CS:
public partial class Window1: Window {
public IEnumerable Value {
get { return (IEnumerable)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(IEnumerable), typeof(Window1), new UIPropertyMetadata(null));
public Window1() {
InitializeComponent();
var view = new TheCollectionView();
view.MoveCurrentTo(null);
this.Value = view;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
MessageBox.Show("Selection changed");
}
}
public class TheCollectionView: CollectionView {
public TheCollectionView(): base(new TheCollection()) {
}
}
public class TheCollection: List<string> {
public TheCollection() {
Add("string1");
Add("string2");
Add("string3");
}
}
这篇关于如何确保没有项目在数据绑定列表框选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!