C# WPF 列表框选择已更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10787415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Listbox SelectionChanged
提问by 123 456 789 0
I currently have an Entitythat has a collection property on it. I want to know if why would the SelectionChangedonly fire once and it won't trigger the SelectionChangedagain once I try to select the item that was previously selected.
我目前有一个Entity带有集合属性的对象。我想知道为什么SelectionChanged只有一次触发并且SelectionChanged一旦我尝试选择以前选择的项目就不会再次触发。
MainWindowViewModel
主窗口视图模型
public MainWindowViewModel()
{
var a = new List<Test>();
a.Add(new Test() { Name = "Leo", Test1 = new List<Test1> { new Test1() { Content = "aaa"} } });
a.Add(new Test() { Name = "2", Test1 = new List<Test1> { new Test1() { Content = "bbb"} } });
a.Add(new Test() { Name = "Le33o", Test1 = new List<Test1> { new Test1() { Content = "ccc"} } });
A = a;
}
private List<Test> _a;
public List<Test> A
{
get { return _a; }
set { _a = value; OnPropertyChanged("A");}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
My Mainwindow
我的主窗口
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
private void Test(object sender, SelectionChangedEventArgs e)
{
}
My listbox structure
我的列表框结构
public class Test
{
public List<Test1> Test1 { get; set; }
public string Name
{
get;set;
}
}
public class Test1
{
public string Content { get; set; }
}
I select the first object, the event fires, I select the second object, the event fires, I select the first object, the event doesn't fire, I select third object, the event fires. It seems like it only triggers and calls the event once.
我选择第一个对象,事件触发,我选择第二个对象,事件触发,我选择第一个对象,事件不触发,我选择第三个对象,事件触发。它似乎只触发并调用一次事件。
My XAMLCode:
我的XAML代码:
<ItemsControl x:Name="Lists" ItemsSource="{Binding A}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold"
Style="{StaticResource DefaultTextBlockStyle}" />
<ListBox SelectionChanged="Test" ItemsSource="{Binding Test1}"
Margin="5,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The test method is just an empty method I just want to hit the breakpoint every time I change.
测试方法只是一个空方法我只想每次更改时都打断点。
private void Test(object sender, SelectionChangedEventArgs e)
{
}
Update 1: I tried to reproduce this in a simple WPF app, it seems that the ListBoxItem is getting IsEnabled to false but I snooped it and all the controls are enabled. It just getting a grey background that looks like disabled. Will try to investigate further.
更新 1:我试图在一个简单的 WPF 应用程序中重现这个,似乎 ListBoxItem 正在将 IsEnabled 设置为 false,但我窥探了它并启用了所有控件。它只是得到一个看起来像残疾人的灰色背景。将尝试进一步调查。
Update 2: It seems that the ListBoxItem IsSelected property is not being unset when you change an item.
更新 2:当您更改项目时,似乎没有取消设置 ListBoxItem IsSelected 属性。
回答by paparazzo
Selecting the same item is not a SelectionChanged event. The selection did not change.
选择相同的项目不是 SelectionChanged 事件。选择没有改变。
The problem statement is not clear.
问题陈述不清楚。
Break it down. This works for me. If I select any item a second time, 3rd, 4th time the event fires.
打破它。这对我有用。如果我第二次选择任何项目,第 3 次、第 4 次事件会触发。
OP asserted it does not work if it is a List in a List. Still works for me.
OP 断言,如果它是列表中的列表,则它不起作用。仍然对我有用。
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
}
public List<ListList> ListList1
{
get { return new List<ListList>{new ListList("name1", new List<string> { "one", "two", "three" })}; }
}
private void Test(object sender, SelectionChangedEventArgs e)
{
ListBox lb = (ListBox)sender;
System.Diagnostics.Debug.WriteLine(lb.SelectedItem.ToString());
}
public class ListList
{
public string Name { get; set; }
public List<string> Values { get; set; }
public ListList(string name, List<string> values) { Name = name; Values = values; }
}
<ListBox ItemsSource="{Binding Path=ListList1}">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBox SelectionChanged="Test" ItemsSource="{Binding Path=Values}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
回答by WiiMaxx
To answer your question ...
要回答你的问题...
I want to know if why would the SelectionChanged only fire once and it won't trigger the SelectionChanged again once I try to select the item that was previously selected.
我想知道为什么 SelectionChanged 只会触发一次,而一旦我尝试选择以前选择的项目,它就不会再次触发 SelectionChanged。
... in a learning by doing way
... 以边做边学的方式
open a new WPF Project add 2 Listboxes create ONE SelectionChanged event for both and add some items to each Listbox
打开一个新的 WPF 项目,添加 2 个列表框,为两者创建一个 SelectionChanged 事件,并向每个列表框添加一些项目
let's mention it look's now like this
让我们提一下它现在看起来像这样
<ListBox Height="100" Name="listBox1" Width="120" SelectionChanged="listBox_SelectionChanged"/>
<ListBox Height="100" Name="listBox2" Width="120" SelectionChanged="listBox_SelectionChanged"/>
.
.
var list = new List<string>();
list.Add("Element1");
list.Add("Element2");
list.Add("Element3");
list.Add("Element4");
listBox1.ItemsSource = list;
listBox2.ItemsSource = list;
If you now select Element1in listBox1your listBox_SelectionChangedget triggert after that select Element2in your listBox2so your listBox_SelectionChangedget's triggert again.
如果你现在Element1在listBox1你的listBox_SelectionChangedget triggert 中选择,然后Element2在你的listBox2所以你的listBox_SelectionChangedget 触发器中再次选择。
If you take a closer look at your listBox1you will see that the Backgroundbehind your Element1is gray which means it is selected, but with out focuse. If you select now the Element1in your listBox1again the listBox_SelectionChangeddoesn't get triggert because the selection does't change only the Focuse does.
如果你仔细看看你的,listBox1你会发现你的Background后面Element1是灰色的,这意味着它被选中了,但没有焦点。如果您现在选择 Element1你listBox1再listBox_SelectionChanged没有得到triggert因为选择简化版,只更改Focuse一样。
That's the exact same "problem" is in your code because your DataTemplatedoes the same think we did as we added our 2 Listboxes just automatically
这与您的代码中存在完全相同的“问题”,因为您DataTemplate的想法与我们自动添加 2 个列表框时的想法相同
as simple and dirt workaround you could use the following code
作为简单而肮脏的解决方法,您可以使用以下代码
private object seletedItem;
private ListBox ItemsHost;
private void Test(object sender, SelectionChangedEventArgs e)
{
var buff = sender as ListBox;
if (seletedItem != null)
if (ItemsHost != buff)
ItemsHost.SelectedItem = null;
ItemsHost = buff;
if (e.AddedItems.Count > 0)
seletedItem = e.AddedItems[0];
}
回答by Karthik Nishanth
The simple solution I found out is to make selectedItemas nullin the event handler.
简单的解决方案,我发现是使selectedItem作为null事件处理程序。
private void tempList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Card selectedOffer = (TempList.SelectedItem as Card);
if (selectedOffer != null)
{
MessageBox.Show(selectedOffer._id);
}
ListBoxNeeded.SelectedItem = null;
}

