![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | Problem with behaviour of MouseEvents on ListViewItems Has anyone tried attaching event handlers to the MouseDown/MouseUp events on a ListViewItem? I'm having problems with the row properly firing MouseLeftButtonDown and MouseDown events. I first observed this in alarger program that I was trying to write so I wrote as simple an example as possible to test: First two classes to represent the collection that we bind to the ListView: public class Model { private int field1; private int field2; public int Field1 { get { return field1; } set { field1 = value; } } public int Field2 { get { return field2; } set { field2 = value; } } } public class ModelCollection : ObservableCollection<Model> { } Then a window, both XAML and code behind: <?Mapping XmlNamespace="local" ClrNamespace="SampleGridView" ?> <Window x:Class="SampleGridView.Window1" xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" xmlns:local="local" Title="SampleGridView" > <Window.Resources> <local:ModelCollection x:Key="coll"> <local:Model Field1="123" Field2="456" /> <local:Model Field1="789" Field2="111" /> </local:ModelCollection> </Window.Resources> <Grid> <ListView x:Name="myGrid" ItemsSource="{StaticResource coll}" /> </Grid> </Window> public partial class Window1 : Window { public Window1() { InitializeComponent(); this.Loaded += OnLoad; } private void OnLoad(object sender, RoutedEventArgs e) { // attach event handlers to each ListViewItem int count = myGrid.Items.Count; for (int i = 0; i < count; i++) { ListViewItem item = (ListViewItem) myGrid.ItemContainerGenerator.ContainerFromIndex(i); item.MouseDown += HandleMouseDown; item.MouseUp += HandleMouseUp; item.MouseLeftButtonDown += HandleMouseLeftButtonDown; item.MouseLeftButtonUp += HandleMouseLeftButtonUp; item.MouseRightButtonDown += HandleMouseRightButtonDown; item.MouseRightButtonUp += HandleMouseRightButtonUp; } } private void HandleMouseDown(object sender, MouseEventArgs e) { Console.WriteLine("MouseDown"); } private void HandleMouseUp(object sender, MouseEventArgs e) { Console.WriteLine("MouseUp"); } private void HandleMouseLeftButtonDown(object sender, MouseEventArgs e) { Console.WriteLine("HandleMouseLeftButtonDown"); } private void HandleMouseLeftButtonUp(object sender, MouseEventArgs e) { Console.WriteLine("HandleMouseLeftButtonUp"); } private void HandleMouseRightButtonDown(object sender, MouseEventArgs e) { Console.WriteLine("HandleMouseRightButtonDown"); } private void HandleMouseRightButtonUp(object sender, MouseEventArgs e) { Console.WriteLine("HandleMouseRightButtonUp"); } } Now when mousing over a row, the following output is observed when clicking: Left Click: HandleMouseLeftButtonUp MouseUp Right Click: HandleMouseRightButtonDown MouseDown HandleMouseRightButtonUp MouseUp It looks to me like the left MouseDown events are not happening. I think this is most likely a bug in WPF, but would someone please prove me wrong and show me why it's a problem with my code? Regards, Jason |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Problem with behaviour of MouseEvents on ListViewItems Hi Jason, I had no time to reproduce your scenario on my machine but did you try to attach your handlers to the PreviewMouseXXX events. Maybe some events are swallowed as an result of event bubbling. Thanks, Chris Jason Dolinger wrote: > Has anyone tried attaching event handlers to the MouseDown/MouseUp > events on a ListViewItem? I'm having problems with the row properly > firing MouseLeftButtonDown and MouseDown events. I first observed this > in alarger program that I was trying to write so I wrote as simple an > example as possible to test: > > First two classes to represent the collection that we bind to the ListView: > > public class Model > { > private int field1; > private int field2; > > public int Field1 > { > get { return field1; } > set { field1 = value; } > } > > public int Field2 > { > get { return field2; } > set { field2 = value; } > } > } > > public class ModelCollection : ObservableCollection<Model> > { > } > > > > Then a window, both XAML and code behind: > <?Mapping XmlNamespace="local" ClrNamespace="SampleGridView" ?> > <Window x:Class="SampleGridView.Window1" > xmlns="http://schemas.microsoft.com/winfx/avalon/2005" > xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" > xmlns:local="local" > Title="SampleGridView" > > > <Window.Resources> > <local:ModelCollection x:Key="coll"> > <local:Model Field1="123" Field2="456" /> > <local:Model Field1="789" Field2="111" /> > </local:ModelCollection> > </Window.Resources> > <Grid> > <ListView x:Name="myGrid" ItemsSource="{StaticResource coll}" /> > </Grid> > </Window> > > > > public partial class Window1 : Window > { > > public Window1() > { > InitializeComponent(); > this.Loaded += OnLoad; > > } > > private void OnLoad(object sender, RoutedEventArgs e) > { > // attach event handlers to each ListViewItem > int count = myGrid.Items.Count; > for (int i = 0; i < count; i++) > { > ListViewItem item = (ListViewItem) > myGrid.ItemContainerGenerator.ContainerFromIndex(i); > item.MouseDown += HandleMouseDown; > item.MouseUp += HandleMouseUp; > item.MouseLeftButtonDown += HandleMouseLeftButtonDown; > item.MouseLeftButtonUp += HandleMouseLeftButtonUp; > item.MouseRightButtonDown += HandleMouseRightButtonDown; > item.MouseRightButtonUp += HandleMouseRightButtonUp; > } > } > > > > private void HandleMouseDown(object sender, MouseEventArgs e) > { > Console.WriteLine("MouseDown"); > } > > private void HandleMouseUp(object sender, MouseEventArgs e) > { > Console.WriteLine("MouseUp"); > } > > private void HandleMouseLeftButtonDown(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseLeftButtonDown"); > } > > private void HandleMouseLeftButtonUp(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseLeftButtonUp"); > } > > private void HandleMouseRightButtonDown(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseRightButtonDown"); > } > > private void HandleMouseRightButtonUp(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseRightButtonUp"); > } > } > > > Now when mousing over a row, the following output is observed when > clicking: > > Left Click: > > HandleMouseLeftButtonUp > MouseUp > > Right Click: > HandleMouseRightButtonDown > MouseDown > HandleMouseRightButtonUp > MouseUp > > > It looks to me like the left MouseDown events are not happening. I > think this is most likely a bug in WPF, but would someone please prove > me wrong and show me why it's a problem with my code? > > Regards, > Jason > > > > > > > > > |
My System Specs![]() |
| | #3 (permalink) |
| | RE: Problem with behaviour of MouseEvents on ListViewItems The reason is in your OnLoad(), the items are not created yet. You will find that count in that function is zero. The right way to do it is: <Style TargetType='{x:Type ListViewItem}' > <EventSetter Event='MouseDown' Handler='HandleMouseDown'/> <EventSetter Event='MouseUp' Handler='HandleMouseUp'/> ...... </Style> <ListView ItemContainerStyle="{StaticResource}"/> "Jason Dolinger" wrote: > Has anyone tried attaching event handlers to the MouseDown/MouseUp > events on a ListViewItem? I'm having problems with the row properly > firing MouseLeftButtonDown and MouseDown events. I first observed this > in alarger program that I was trying to write so I wrote as simple an > example as possible to test: > > First two classes to represent the collection that we bind to the ListView: > > public class Model > { > private int field1; > private int field2; > > public int Field1 > { > get { return field1; } > set { field1 = value; } > } > > public int Field2 > { > get { return field2; } > set { field2 = value; } > } > } > > public class ModelCollection : ObservableCollection<Model> > { > } > > > > Then a window, both XAML and code behind: > <?Mapping XmlNamespace="local" ClrNamespace="SampleGridView" ?> > <Window x:Class="SampleGridView.Window1" > xmlns="http://schemas.microsoft.com/winfx/avalon/2005" > xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" > xmlns:local="local" > Title="SampleGridView" > > > <Window.Resources> > <local:ModelCollection x:Key="coll"> > <local:Model Field1="123" Field2="456" /> > <local:Model Field1="789" Field2="111" /> > </local:ModelCollection> > </Window.Resources> > <Grid> > <ListView x:Name="myGrid" ItemsSource="{StaticResource coll}" /> > </Grid> > </Window> > > > > public partial class Window1 : Window > { > > public Window1() > { > InitializeComponent(); > this.Loaded += OnLoad; > > } > > private void OnLoad(object sender, RoutedEventArgs e) > { > // attach event handlers to each ListViewItem > int count = myGrid.Items.Count; > for (int i = 0; i < count; i++) > { > ListViewItem item = (ListViewItem) > myGrid.ItemContainerGenerator.ContainerFromIndex(i); > item.MouseDown += HandleMouseDown; > item.MouseUp += HandleMouseUp; > item.MouseLeftButtonDown += HandleMouseLeftButtonDown; > item.MouseLeftButtonUp += HandleMouseLeftButtonUp; > item.MouseRightButtonDown += HandleMouseRightButtonDown; > item.MouseRightButtonUp += HandleMouseRightButtonUp; > } > } > > > > private void HandleMouseDown(object sender, MouseEventArgs e) > { > Console.WriteLine("MouseDown"); > } > > private void HandleMouseUp(object sender, MouseEventArgs e) > { > Console.WriteLine("MouseUp"); > } > > private void HandleMouseLeftButtonDown(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseLeftButtonDown"); > } > > private void HandleMouseLeftButtonUp(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseLeftButtonUp"); > } > > private void HandleMouseRightButtonDown(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseRightButtonDown"); > } > > private void HandleMouseRightButtonUp(object sender, > MouseEventArgs e) > { > Console.WriteLine("HandleMouseRightButtonUp"); > } > } > > > Now when mousing over a row, the following output is observed when clicking: > > Left Click: > > HandleMouseLeftButtonUp > MouseUp > > Right Click: > HandleMouseRightButtonDown > MouseDown > HandleMouseRightButtonUp > MouseUp > > > It looks to me like the left MouseDown events are not happening. I > think this is most likely a bug in WPF, but would someone please prove > me wrong and show me why it's a problem with my code? > > Regards, > Jason > > > > > > > > > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Problem with behaviour of MouseEvents on ListViewItems Ji Zhang [MSFT] wrote: > The reason is in your OnLoad(), the items are not created yet. You will find > that count in that function is zero. > > The right way to do it is: > <Style TargetType='{x:Type ListViewItem}' > > <EventSetter Event='MouseDown' Handler='HandleMouseDown'/> > <EventSetter Event='MouseUp' Handler='HandleMouseUp'/> > ...... > </Style> > > <ListView ItemContainerStyle="{StaticResource}"/> > > "Jason Dolinger" wrote: > Thanks Ji. The EventSetter actually answers one of the principal questions that I had about my approach that was left hanging in an earlier thread (applying styles to a GridViewColumn). I wasn't sure how to tell WPF to attach those events to the ListViewItems because I couldn't get a handle on them in the C# code, but this is perfect. But..... While admit that approach is cleaner, the LeftMouse clicks still don't fire the MouseDown or MouseLeftButtonDown events. So I still think there is a problem there. Thanks, Jason |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Problem with behaviour of MouseEvents on ListViewItems You can hook the event in the OnLoaded() through code: listView.AddHander(ListViewItem.MouseLeftButtonDown, new MouseButtonEventHander(HandleMouseRightButtonDown), true); Please make sure that don't skip "true" here. So, the most effective way to hook event on ListViewItem is to do this in OnLoaded() as above. "Jason Dolinger" wrote: > Ji Zhang [MSFT] wrote: > > The reason is in your OnLoad(), the items are not created yet. You will find > > that count in that function is zero. > > > > The right way to do it is: > > <Style TargetType='{x:Type ListViewItem}' > > > <EventSetter Event='MouseDown' Handler='HandleMouseDown'/> > > <EventSetter Event='MouseUp' Handler='HandleMouseUp'/> > > ...... > > </Style> > > > > <ListView ItemContainerStyle="{StaticResource}"/> > > > > "Jason Dolinger" wrote: > > > > Thanks Ji. > The EventSetter actually answers one of the principal questions that I > had about my approach that was left hanging in an earlier thread > (applying styles to a GridViewColumn). I wasn't sure how to tell WPF to > attach those events to the ListViewItems because I couldn't get a handle > on them in the C# code, but this is perfect. But..... > > While admit that approach is cleaner, the LeftMouse clicks still don't > fire the MouseDown or MouseLeftButtonDown events. So I still think > there is a problem there. > > Thanks, > Jason > |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| UAC bizzare behaviour | Vista General | |||
| Strange behaviour! | Vista account administration | |||
| Odd Taskbar Behaviour | Vista General | |||
| Very odd startup behaviour? | Vista General | |||
| Very Odd behaviour | PowerShell | |||