WPF Listbox + Expander events -
I have an extension in the ItemTemplate of a ListBox. Render is right, the issue I have raised is that I would like to select listboxes, when the extension is expanded and / or selected. The MouseDown event does not look like a bubble in the listbox.
What I want is the selected index of listboxes because ListBox_SelectionChanged is not removed, the index is -1 and I can not determine which item is selected.
The ListBox_SelectionChanged Event is removed if the user clicks on the extension's content after its expansion. If they only click on the expander, then the event is not removed. It is misleading the user because they think that they already click on the item when they actually click on the expander header, I need to select the list box when the user extends the extension as long as The user is concerned, then that item is now selected, when it is not actually.
How does this work or how it works
Simplified code for reference:
& lt; Window x: orbit = "wpfApplication3.Window1" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" window 1 "height =" 300 "width =" 300 "loaded =" window_loaded "> & Lt; Grid name = "root" & gt; & Lt; ScrollViewer & gt; & Lt; Listback selection changes = "listblock" option "changed" items source = "{binding}" & gt; & Lt; Itemscontrol.itmateplate & gt; & Lt; DataTemplate & gt; & Lt; Border & gt; & Lt; Expander & gt; & Lt; Expander.Header & gt; & Lt; Text block text = "{binding path = name}" /> & Lt; /Expander.Header> & Lt; Expander.Content & gt; & Lt; StackPanel & gt; & Lt; Text block text = "{binding path = age}" /> & Lt; Text block text = "row2" /> & Lt; Text block text = "row 3" /> & Lt; / StackPanel & gt; & Lt; /Expander.Content> & Lt; / Expander & gt; & Lt; / Border & gt; & Lt; / DataTemplate & gt; & Lt; /ItemsControl.ItemTemplate> & Lt; / ListBox & gt; & Lt; / ScrollViewer & gt; & Lt; / Grid & gt; & Lt; / Window & gt;
Simple class for binding:
public class person {public string name {get; Set; } Public int Age {Received; Set; Creating and Populating Data for Binding: Private zero-window-loaded (Object Sender, Routing, and E.E.G. E) {Data = New Observe Collection & lt; Person & gt; ); Data Add (new person {name = "one", age = 10}); Data Add (new person {name = "two", age = 20}); Data Add (new person {name = "three", age = 30}); Route. DataContext = Data; }
This is the event that I want (actually only selected indax is required)
Private Zero ListBox_SelectionChanged (Object Sender, SelectionChangedEventArgs E) { ListBox Box = (ListBox) this; // This value is not set because the phenomena of the expander does not move upwards from the fire, select changelog event int index = box. Selected indices; }
An alternative method that depends on ISSelected, you expanded / behind the code Use the following code to find out the compressed event and the listbox index on which you clicked.
DependencyObject dep = (DependencyObject) e.OriginalSource; While ((dep! = Null) & amp; (Dep listViewItem) {dep = visualTreeHelper.GetParent (dep); } If (DP == blank) return; Int Index = yourListBox.ItemContainerGenerator.IndexFromContainer (DP);
Comments
Post a Comment