Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug ListBoxItem don´t trigger MouseLeftButtonDown in Beta 2
45 replies. Latest Post by rdlecler on April 10, 2009.
(1)
Lluis
Member
18 points
7 Posts
06-07-2008 6:23 AM |
Is this a bug?
Greetings
sgzwkrm
24 points
11 Posts
06-07-2008 11:16 AM |
And I have found another bug is...Button don't trigger MouseLeftButtonDown\Up in Beta2...faint...
eduardot
6 points
3 Posts
06-07-2008 9:11 PM |
The event is not getting fire because Button class is marking the event as Handle but you should be able to use Click event.
Thanks
Eduardo
06-07-2008 10:33 PM |
OK, I have read this following in the help document:
Certain control classes (for example Button) provide control-specific handling for mouse events such as MouseLeftButtonDown. The control-specific handling typically involves handling the event at a class level rather than at the instance level, and marking the MouseLeftButtonDown event data's Handled value as true such that the event cannot be handled by instances of the control class, nor by other elements (at a class or instance level) anywhere further along the event route. In the case of Button, the class design does this so that the event Click can be raised instead.
But I think Click can't substitute Down\Up...
06-09-2008 8:22 AM |
I understand this behaviour in button, but not in ListboxItem. Also breaks compatibility with WPF
sladapter
All-Star
17439 points
3,172 Posts
06-09-2008 5:02 PM |
Now in beta 2 MouseLeftButtonDown/MouseLeftButtonUp events no longer fire for Button/ListboxItem etc. How do we do Drag/Drop with those controls if we can not catch the MouseDown/Up event?
CleverCoder
203 points
157 Posts
06-09-2008 5:49 PM |
I'd like to add that this also seems to be a problem when clicking a cell in the DataGrid. I can no longer respond to a cell being clicked!
Lyynx
79 points
29 Posts
06-10-2008 4:39 AM |
I've discovered the same problem with the slider control. It no longer raises MouseLeftButtonDown, MouseLeftButton up or quite possibly any of the Mouse events.
I can raise ValueChanged no problems.
Also I tried a MouseLeftButtonDown on the container grid thinking maybe it was catching it and found that I could *sometimes* get it to be raised depending where I clicked. Most of the time it did not... (could have been only raising if I was exactly over the gridlines or something weird? dunno)
06-10-2008 10:45 AM |
I'm not religous, but dear god. I hope they fix these quirks and release another beta SOON! This is NOT good. I'm having so much headache with the DataGrid, it's starting to worry me. Isn't the goal to fix more bugs, and not introduce new ones? yikes.
Maybe if the control that "Handled" the event was smart enough to see "Gee,.. I have the same event that people might subscribe to.." and NOT digest the thing.. because the Grid has a MouseButton Down, I would expect to ALWAYS get the event as a consumer of the grid..
06-10-2008 11:01 AM |
I have the same feeling. I think they rushed. Not only they did not fix a lot old problems, they break even more. The old problems I can still live with if they did not break my work-around. But now some those work-around are also broken.
06-10-2008 3:27 PM |
Well, for the record, I am now at the point where I'm trying to fix an issue with a listbox. Guess what it involves. MouseLeftButtonDown! In my case, it's a list box inside of a popup, that when clicked, will close the popup. It seems that SelectionChanged works.. but with one item, the selection will never change... And of course, MouseLeftButtonDown does not do a darn thing.
Crossing fingers that they can fix this SOON.
snelldl
148 points
141 Posts
06-10-2008 3:34 PM |
I've did the same thing with selectionchanged, but when I try to close the popup, I get an error and my form goes blank.
06-10-2008 4:03 PM |
Are you still getting error when try to close the popup in SelectionChanged event? I was hoping they fixed that one. I haven't tried it yet. I have a workaround in beta 1 for this problem and hoping I don't have to use that anymore after beta 2.
That's what I mean they did not fix many old problems we reported, but just break more.
06-10-2008 4:38 PM |
Funny.. I tried the same thing, with the same result. (CLosing a popup when an item in the list is selected) BOOM! Crash!
I found a workaround was to create a DispatcherTimer (50 ms) and close the popup from there. So far, that has worked. NOw I'm trying to battle some other quirk involving a missing watermarked text box... grrrr...
06-10-2008 4:42 PM |
Yes, use timer is the work-around. So we have to keep that.
sensenet...
2 points
1 Posts
06-11-2008 9:13 AM |
Nope, it's not a bug, it's just a new way of handling pointing devices, because I guess mouse is not the primary input device in Silverlight anymore. Since then the controls became Click event orientated.Here's a quick&dirty workaround (without indepth analysis of the unseen side effects!), but it provides an easy solution for those who want to use the MouseDown/Up events in the old fashioned way.public class MyButton : Button{ public MyButton() { this.MouseLeftButtonDown += new MouseButtonEventHandler(MyButton_MouseLeftButtonDown); this.MouseLeftButtonUp += new MouseButtonEventHandler(MyButton_MouseLeftButtonUp); } void MyButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Content = "mouse down"; }
Nope, it's not a bug, it's just a new way of handling pointing devices, because I guess mouse is not the primary input device in Silverlight anymore. Since then the controls became Click event orientated.
Hope it helps.
06-11-2008 9:32 AM |
Man, it sounds like were building similar apps!
What's the code to create that timer and where do you place it?
06-11-2008 9:46 AM |
Nevermind. I figured it out from an example in the SL chm.
saidhabanu
06-11-2008 9:50 AM |
......
Even this approach didnt work as expected
Petunya
42 points
35 Posts
06-11-2008 5:00 PM |
CleverCoder:Well, for the record, I am now at the point where I'm trying to fix an issue with a listbox. Guess what it involves. MouseLeftButtonDown! In my case, it's a list box inside of a popup, that when clicked, will close the popup. .....
Well, for the record, I am now at the point where I'm trying to fix an issue with a listbox. Guess what it involves. MouseLeftButtonDown! In my case, it's a list box inside of a popup, that when clicked, will close the popup. .....
I am having a similar, strange issue. I'm not sure if this is the thread to post it, since it is slightly different. For Beta 1 I followed Shawn Burke's tutorial for creating an "ExpandoHeaderControl" and then templated it so it ended up looking more like a typical drop-down control. The problem I ran into was that the drop-down content was being hidden behind a datagrid, instead of showing on top. I tried several ways of getting it to be on top - tried setting canvas.zindex, tried adding it as the last control, etc. I was hoping that something magical would happen in Beta 2 and one of those methods would fix the problem. I was wrong.
So, I saw somebody suggest using a Popup. I converted to that today, though I did not like having to set the vertical offset directly in my template... I tried doing so using template binding, to make my control more re-usable, but it did not work.
To get to the point, I have a listbox inside of a popup now, and the SelectionChanged event is only firing the first time an item is selected. Meaning, if I select itemA, then select itemB, everything is working fine, until I try to select itemA again - nothing happens.
I don't know if it's any help, but I am successfully closing the popup in my selection changed event handler (for the times that it actually fires ).
06-11-2008 5:44 PM |
SelectionChanged event only fires (as the event name indicated) when selection changed. If you click on the item that is already selected, this event won't fire because the selection did not change. That is the same behavior since beta1.
If you do not want that behavior, in beta 1 you can catch ListBox.MouseDown event. In beta 2 they took that event away. But Grid still can fire MouseDown event, so if you wrap your List Item in a Grid and hookup MouseDown event on the Grid you can still catch it.
Here is how you do it:
<ListBox x:Name="List" > <ListBox.ItemTemplate> <DataTemplate> <Grid Background="Transparent" MouseLeftButtonDown="Item_MouseLeftButtonDown" Cursor="Hand" Loaded="Item_Loaded"> <TextBlock x:Name="Text" Margin="5, 5, 5, 5" Text="{Binding Text}" FontSize="12" /> </Grid> </DataTemplate> </ListBox.ItemTemplate></ListBox>
private void Item_Loaded(object sender, RoutedEventArgs e) { FrameworkElement g = sender as FrameworkElement; g.Width = Menu.ActualWidth; // this will make the Grid stretch to the whole width of the ListBox } private void Item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //Do things you want to do here when you click a your list item }
06-11-2008 7:26 PM |
Thank you for your response, I think I might be able to come up with a work around based on this.
However, I want to point out that the problem is that the selection changed event is not firing when it is supposed to. I am not trying to have it fire when clicking on the selected item. If I select A, then select B, B is the current selection, and clicking A should fire the selection changed event, but it does not.
It's not firing for items that were previously selected, even if they are not currently selected. Just re-reading this it sounds confusing, but the basic functionality of a listbox firing the selection changed event when an item other than the selected item is clicked, is not working under a certain circumstance. It's almost like the listbox is "remembering" all the selections I have made and only firing selection changed if I click something new.
06-11-2008 9:10 PM |
Sorry I did not read you post carefully.
But I just tested my Listbox, it does not have the problem you are having. My listbox is like the one I just posted. But I use SelectionChanged event, not the mousedown event. If you could post your Xaml I'll see if I can reproduce the problem.
06-12-2008 10:55 AM |
No need to be sorry, I am having a hard time explaining in a way that isn't confusing (for me, at least). I am going to try using the mouse down event and see if I can get around my problem.
I posted snippets of code and the full xaml for the listbox here if you would like to view it. I am wondering if the issue has something to do with the listbox being inside of a popup, or the popup being closed before the selection changed event handler has finished its business, etc. The listbox behaves normally under normal conditions, and though I wasn't noticing the issue before, I didn't notice it until after I put my drop-down content inside a popup.
Virrpanna
26 points
06-17-2008 1:47 AM |
This is really confusing!
Listbox doesn’t trigger MouseLeftButtonDown anymore, but still triggers MouseLeftButtonUp. In some situations they are of course interchangeable.
I really would like to know hove the silverlight team developers have reasoned, since it would give me a clue hove to think and design the apps for the future release.
yaip
160 points
132 Posts
06-17-2008 9:59 PM |
I am faced with the same problem but not with a ListBox but a HyperLinkButton in an ItemsControl. I have following xaml:
<ItemsControl x:Name="itmStudents" Margin="0,0,0,0" Canvas.Top="63.287" Canvas.Left="172.2" Padding="10" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<HyperlinkButton x:Name="hlbButton" FontSize="16" ></HyperlinkButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
and following in my code-behind:
Private Sub itmStudents_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles itmStudents.MouseLeftButtonDown
btnSubmit.Opacity = 0
txtStudent.Opacity = 0
lblStudent.Opacity = 0
Me.itmStudents.Opacity = 0
txtMessage.Text = "Please wait..."
Dim textB As TextBlock = e.Source
Dim linkB As HyperlinkButton = textB.DataContext
Dim s As String = linkB.Tag
Dim asmx2 As New ServiceReference2.GetStudentArt_WSSoapClient
AddHandler asmx2.GetStudentArtCompleted, AddressOf asmx2_GetStudentArtWithAsmxCompleted
asmx2.GetStudentArtAsync(Convert.ToInt32(linkB.Tag))
End Sub
But this won't work.
06-18-2008 11:04 AM |
Yaip,
I think you may be able to work around this problem. Instead of relying on the item's mouse down event, hook into the HyperlinkButton's click event. In that case your method should only change slightly, with "sender" being the HyperlinkButton. Below I have put the XAML and VB, though I don't know VB too well so please forgive any mistakes.
<HyperlinkButton x:Name="hlbButton" FontSize="16" Click="hlbButton_Click" ></HyperlinkButton>
Private Sub hlbButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles hlbButton.Click
Dim linkB As HyperlinkButton = sender
06-18-2008 6:14 PM |
Petunya: Private Sub hlbButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles hlbButton.Click
Since the HyperLinkButton is in ItemsControl, it is not available in the "handles" statement as you have shown.
06-18-2008 6:30 PM |
Sorry, I am unfamiliar with VB. However, you should be able to somehow use the hlbButton_Click method as the HyperlinkButton's event handler, correct? I know you can do it in C# by simply saying in the XAML of the HyperlinkButton: Click="hlbButton_Click", no "Handles" statement necessary. I wasn't sure how to translate to VB.
06-18-2008 6:36 PM |
I figured as much. I removed the "handles" statement. Still won't trap :(
Dimasina
06-23-2008 11:53 AM |
It is a bug.
After I got the same problem I tried to use MouseLeftButtonUp. Why not?
It works. Good.
Oops! It is not good. It calls my method twice. Why? I believe that double call and bug in MouseLeftButtonDown are connected.
idragoev
10 points
6 Posts
06-24-2008 9:53 AM |
Hi,
I faced same problem with left mouse button when styling ListBoxItems. See the Tip: Force firing MouseLeftButtonDown event on any place in a StackPanel. The trick I used is to add Background="Transparent" on the underlying control so now the left mouse button click is handled correctly.
06-24-2008 12:37 PM |
I tried that also. Doesn't work if the HyperlinkButton is in ItemsControl.
Silverli...
Participant
1133 points
193 Posts
06-25-2008 2:17 AM |
I did a small example and it works just fine. Here the xaml and the source:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightTestApp.Page" Width="640" Height="480" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"> <StackPanel> <ItemsControl x:Name="lbItems"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel x:Name="RootElement" Background="Beige"> <TextBlock Text="{Binding Text}"/> <HyperlinkButton FontSize="16" NavigateUri="{Binding Link}" Content="{Binding Link}" Click="HyperlinkButton_Click"/> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> <TextBlock x:Name="clickedItem" /> </StackPanel> </UserControl>
1: using System;
2: using System.Windows;
3: using System.Windows.Controls;
4: using System.Windows.Documents;
5: using System.Windows.Ink;
6: using System.Windows.Input;
7: using System.Windows.Media;
8: using System.Windows.Media.Animation;
9: using System.Windows.Shapes;
10: using System.Collections.Generic;
11:
12: namespace SilverlightTestApp
13: {
14: public partial class Page : UserControl
15: {
16: public Page()
17: {
18: // Required to initialize variables
19: InitializeComponent();
20:
21: this.lbItems.ItemsSource = new List<TestClass>(){
22: new TestClass(){Text="SilverlightShow.net", Link="http://www.silverlightshow.net"},
23: new TestClass(){Text="Silverlight.net", Link="http://www.silverlight.net"},
24: new TestClass(){Text="CompletIT.com", Link="http://www.completit.com"},
25: new TestClass(){Text="Google.com", Link="http://www.google.com"}};
26: }
27:
28: private void HyperlinkButton_Click( object sender, RoutedEventArgs e )
29: {
30: this.clickedItem.Text = ( ( HyperlinkButton )e.Source ).Content.ToString();
31: }
32: }
33:
34: public class TestClass
35: {
36: public string Text
37: {
38: get;
39: set;
40: }
41:
42: public string Link
43: {
44: get;
45: set;
46: }
47: }
48: }
Rizwan S...
21 points
23 Posts
06-25-2008 3:23 AM |
I'm having the same issue where I need to handle MouseLeftButtonUp of ScrollBar control... Why would I need Scroll event only which will force me to query data repeatedly on short intervals?
I want to query database only when user moves the slider to the place of his/her choice and then release the mouse....
I hope to get a solution :(
Best Regards,
Flyte
14 points
8 Posts
06-26-2008 5:29 PM |
sensenet_silverteam: Nope, it's not a bug, it's just a new way of handling pointing devices, because I guess mouse is not the primary input device in Silverlight anymore. Since then the controls became Click event orientated.Here's a quick&dirty workaround (without indepth analysis of the unseen side effects!), but it provides an easy solution for those who want to use the MouseDown/Up events in the old fashioned way.public class MyButton : Button{ public MyButton() { this.MouseLeftButtonDown += new MouseButtonEventHandler(MyButton_MouseLeftButtonDown); this.MouseLeftButtonUp += new MouseButtonEventHandler(MyButton_MouseLeftButtonUp); } void MyButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Content = "mouse down"; } void MyButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.Content = "mouse up"; } protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); e.Handled = false; } protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); e.Handled = false; }} Hope it helps.
void MyButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Content = "mouse down"; }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); e.Handled = false; }
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); e.Handled = false; }}
Hi, I'd like to try and implement this workaround, but I can't figure out how to create instances of our own classes using the XAML. For example, with HTML and CSS, you'd tell the HTML element whichever CSS class you'd like it to be. This is not the case with XAML and C#.
I am trying to use MouseLeftButtonDown and up on a slider, so I would like to create a class which implements the Slider class and override it's behavior like above. It's just like I say, I don't know how to create an instance of my custom class in XAML.
Could anybody please help?
Thanks :)
06-26-2008 5:42 PM |
Try putting a HyperlinkButton within Items.Control and trap the MouseLeftButtonDown event. It won't work.
06-26-2008 7:34 PM |
Also, this method does not work for a Slider type..
Error 1 'BBOPreviewPlayer.MySlider.OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs)': no suitable method found to override
Error 2 'BBOPreviewPlayer.MySlider.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs)': no suitable method found to override
Dave Bri...
681 points
229 Posts
07-07-2008 5:13 PM |
I have fighting this same issue for a while now. The best solution seems to be inheriting the slider and exposing your own events as needed. Something like this:
{
thumb.DragStarted +=
}
ThumbDragStarted(sender, e);
ThumbDragCompleted(sender, e);
07-07-2008 5:26 PM |
Hi Dave, thanks for the response.
I would like to try your method, but I have not been able to find out how to create instances of my own classes in the XAML. I've searched for this and been unsuccessful in finding any answers. I have also asked in various forums and not been met with any success.
Is there a tutorial anywhere which shows how this is done, or if it's simple, would you be able to tell me?
Thanks again.
07-08-2008 3:15 AM |
"I would like to try your method, but I have not been able to find out how to create instances of my own classes in the XAML."
Simply Open control browse dialog in expression blend, go to custom controls tab and select your control from the list. Now your control should be added to tool box in expression blend.
Double click the control and it will be added on your UI.
I hope this helps,
07-08-2008 5:02 AM |
Flyte:I would like to try your method, but I have not been able to find out how to create instances of my own classes in the XAML. I've searched for this and been unsuccessful in finding any answers. I have also asked in various forums and not been met with any success. Is there a tutorial anywhere which shows how this is done, or if it's simple, would you be able to tell me?
Hi, Flyte! Take a look at this article about how to create a custom control - http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx - and if you still have questions don't hesitate to ask!
07-08-2008 9:23 AM |
Ah, excellent - thank you both for that!
07-08-2008 10:35 AM |
Let me know how it works out.
I have been 100% in the 1.0 SL word up until now, so this is all new stuff to me too. Cool stuff though.
weitzhan...
23 points
18 Posts
02-19-2009 9:44 AM |
I thought WPF is better!
These Microsoft twists are disgusting!!!
Why do they matther if there will be as many handler as possible!!!!
It doesn't cost any performance, I they're just Gothamites
this pisses me off
rdlecler
4 points
4 Posts
04-10-2009 4:56 PM |
Mouse events in ScrollBar were not being triggered for me either. However, if you hook up the Scroll event the ScrollEventArgs has a enumberation data member called ScrollEventType which comes from System.Windows.Controls.Primitives. If you mouseclick on the scrollbar nothing happens. If you move you get "ThumbTrack" and whenever you lift your mouse you get "EndScroll". So if you just click UpDown, you will just get an EndScroll event. You may be able to use the scroll bar this way to control other controls. Best RDL