Skip to main content

Microsoft Silverlight

Problem with Message Passing / Event HandlingRSS Feed

(0)

jsawruk
jsawruk

Member

Member

0 points

9 Posts

Problem with Message Passing / Event Handling

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
bryant

Star

Star

9447 points

1,558 Posts

Silverlight MVP

Re: Problem with Message Passing / Event Handling

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.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

jsawruk
jsawruk

Member

Member

0 points

9 Posts

Re: Problem with Message Passing / Event Handling

 I checked sender, and that to is simply coming from the wrong control.

bryant
bryant

Star

Star

9447 points

1,558 Posts

Silverlight MVP

Re: Problem with Message Passing / Event Handling

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.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

jsawruk
jsawruk

Member

Member

0 points

9 Posts

Re: Problem with Message Passing / Event Handling

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.

bryant
bryant

Star

Star

9447 points

1,558 Posts

Silverlight MVP

Re: Problem with Message Passing / Event Handling

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.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

jsawruk
jsawruk

Member

Member

0 points

9 Posts

Re: Problem with Message Passing / Event Handling

 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.

jsawruk
jsawruk

Member

Member

0 points

9 Posts

Re: Problem with Message Passing / Event Handling

 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...

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities