Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Problem with Message Passing / Event Handling
7 replies. Latest Post by jsawruk on January 12, 2009.
(0)
jsawruk
Member
0 points
9 Posts
01-08-2009 11:59 AM |
I am not sure if this is a bug or not, but I am getting strange behavior with some events.
I have a StackPanel. Inside of the StackPanel are a number of Grids. Each Grid is one row high and two columns wide. The first column contains a graphic, and the second column contains text, like this:
(img) Label 1(img) Label 2
etc.
(I could in theory use StackPanels instead of Grids, but I have decided to use Grids).
The XAML is:
<Grid x:Name="LayoutRoot" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown" Height="20" Width="250"> <Grid.RowDefinitions> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="20" /> <ColumnDefinition Width="5" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Rectangle Height="5" Width="20" Fill="Black" Grid.Column="0" Grid.Row="0" x:Name="Rect"></Rectangle> <TextBlock Text="" Grid.Column="2" Grid.Row="0" x:Name="txtLabel"></TextBlock> </Grid>
A user clicks on any of the text or images, triggering further events.
Here's the problem
Let's say there are three grids: 1, 2, 3.
A user clicks on the top most grid (#1).
But in the MouseLeftButtonDown handler, the value of e.OriginalSource says that the click originated from grid #3.
Clearly this causes all sorts of unusual behavior. I have successfully reproduced this several times, but have yet to understand what is causing this to occur. Any help would be greatly appreciated.
bryant
Star
9447 points
1,558 Posts
01-08-2009 4:40 PM |
Hmm.. That is interesting. What if you check the value of sender instead of e.OriginalSource? I generally always use the sender if I want to determine what was clicked.
01-08-2009 4:46 PM |
I checked sender, and that to is simply coming from the wrong control.
01-08-2009 4:48 PM |
Hmm.. Then there is probably something going on with your layout that isn't expected. I'd try putting some backgrounds on the grids to see how much space they are actually taking up. Sounds like somehow the bottom Grid is overlapping the top Grid and causing it to fire the click event.
01-08-2009 5:05 PM |
I too thought it might be a layout issue, but so far I haven't figured out how or what might be causing that. I explicitly set dimensions on the Grid objects. I tried setting different backgrounds, but they all seem to fit like they should. I'll have to keep experimenting though. Maybe I should convert the Grids into StackPanels.
01-08-2009 5:16 PM |
Can you post the full Xaml and code that you're using to replicate this (simplified if possible)? I'd be curious to see it and debug it.
01-09-2009 11:07 AM |
New data:
ActualHeight and ActualWidth are reporting correctly.
If I change the Grid to a StackPanel, I still get the error.
As far as XAML and C# code, I'll try to put together a simplified example.
01-12-2009 10:21 AM |
I have created a sample Silverlight app, but I don't know how to to attach it to this post. If you email me, I'll send it to you.
I could NOT reproduce it within the sample project though. Something weird is going on. I can't release the source code of the project that is giving me the problem though...