WPF Listbox + Expander事件 [英] WPF Listbox + Expander events
问题描述
我需要的是ListBox的SelectedIndex。因为ListBox_SelectionChanged没有被触发,索引是-1,我无法确定选择了哪个项目。
如果用户点击内容,则会启动ListBox_SelectionChanged事件的扩张器。如果他们只点击扩展器,则不会触发该事件。这是令人困惑的用户,因为在视觉上,他们认为他们已经点击了该项目实际点击扩展头。当用户展开扩展器时,我需要选择ListBox项,因为就用户而言,现在选择的项目真的不是。
任何建议关于如何让这个工作或替代方法确定列表框中的扩展器的SelectedIndex?
简化代码供参考:
< Window x:Class =WpfApplication3.Window1
xmlns =http://schemas.microsoft.com/winfx/2006/ xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =Window1Height =300Width =300
Loaded =Window_Loaded>
< Grid Name =Root>
< ScrollViewer>
< ListBox SelectionChanged =ListBox_SelectionChangedItemsSource ={Binding}>
< ItemsControl.ItemTemplate>
< DataTemplate>
< Border>
< Expander>
< Expander.Header>
< TextBlock Text ={Binding Path = Name}/>
< /Expander.Header>
< Expander.Content>
< StackPanel>
< TextBlock Text ={Binding Path = Age}/>
< TextBlock Text =Line 2/>
< TextBlock Text =Line 3/>
< / StackPanel>
< /Expander.Content>
< / Expander>
< / Border>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ListBox>
< / ScrollViewer>
< / Grid>
< / Window>
Binding的简单类:
public class Person
{
public string Name {
get;
设置;
}
public int Age {
get;
设置;
}
}
创建和填充绑定数据:
private void Window_Loaded(object sender,RoutedEventArgs e){
data = new ObservableCollection< Person>();
data.Add(new Person {
Name =One,
Age = 10
});
data.Add(new Person {
Name =Two,
Age = 20
});
data.Add(new Person {
Name =Three,
Age = 30
});
Root.DataContext = data;
}
这是我需要的事件(真的只是我需要的SelectedIndex) p>
private void ListBox_SelectionChanged(object sender,SelectionChangedEventArgs e){
ListBox box =(ListBox)sender;
//此值未设置,因为Expander中的事件没有冒泡起火以启动SelectionChanged Event
int index = box.SelectedIndex;
}
在IsSelected上,您可以将展开器的Expanded / Collapsed事件挂接到后面的代码中,并使用以下代码找出您单击的ListBox索引。
DependencyObject dep =(DependencyObject)e.OriginalSource;
while((dep!= null)&((dep是ListViewItem))
{
dep = VisualTreeHelper.GetParent(dep);
}
if(dep == null)
return;
int index = yourListBox.ItemContainerGenerator.IndexFromContainer(dep);
I have an Expander in the ItemTemplate of a ListBox. Renders fine. The issue I have run into is that I would like the ListBox_SelectionChanged event to fire when the expander is expanded and/or selected. The MouseDown event does not seem to bubble up to the ListBox.
What I need is the SelectedIndex of the ListBox. Because the ListBox_SelectionChanged does not get fired, the index is -1 and I cannot determine which item has been selected.
The ListBox_SelectionChanged Event is fired if a user clicks on the Content of the Expander after it has been expanded. If they only click on the expander, the event is not fired. This is confusing to the user because visually, they think they have already clicked on that item when actually clicking on the Expander Header. I need the ListBox Item selected when the user Expands the Expander because as far as the user is concerned, the item is now selected when it really isn't.
Any suggests on how to get this to work or alternate ways of determining the SelectedIndex of the list box with expanders in it?
Simplified code for reference:
<Window x:Class="WpfApplication3.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"
Loaded="Window_Loaded">
<Grid Name="Root">
<ScrollViewer>
<ListBox SelectionChanged="ListBox_SelectionChanged" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate >
<DataTemplate>
<Border>
<Expander>
<Expander.Header>
<TextBlock Text="{Binding Path=Name}"/>
</Expander.Header>
<Expander.Content>
<StackPanel>
<TextBlock Text="{Binding Path=Age}"/>
<TextBlock Text="Line 2"/>
<TextBlock Text="Line 3"/>
</StackPanel>
</Expander.Content>
</Expander>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
</Window>
Simple class for Binding:
public class Person
{
public string Name {
get;
set;
}
public int Age {
get;
set;
}
}
Creating and populating the data for binding:
private void Window_Loaded(object sender, RoutedEventArgs e) {
data = new ObservableCollection<Person>();
data.Add(new Person {
Name = "One",
Age=10
});
data.Add(new Person {
Name = "Two",
Age = 20
});
data.Add(new Person {
Name = "Three",
Age = 30
});
Root.DataContext = data;
}
This is the event I need (really just the SelectedIndex I need)
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
ListBox box = (ListBox)sender;
// This value is not set because events from Expander are not bubbled up to fire SelectionChanged Event
int index = box.SelectedIndex;
}
An alternate way which doesnt depends on the IsSelected, You can hook an Expanded/Collapsed event of expander to the code behind and use the following code to find out the ListBox index on which you clicked.
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is ListViewItem))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
int index = yourListBox.ItemContainerGenerator.IndexFromContainer(dep);
这篇关于WPF Listbox + Expander事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!