We’ve secretly changed this control’s DataContext – let’s see if it notices! [Workaround for a Silverlight data binding bug affecting various scenarios – including DataGrid+ContextMenu]


This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments (5)
  1. leifre says:

    Maybe Im reading this wrong, but i was a bit confused by your example.  You state:

    The parent of the target element data context changes, its an issue.  If the DataContext of the element itself changes, the change will be propagated correctly.

    Your example:

    <Grid x:Name="SimpleBroken" Grid.Column="0" Grid.RowSpan="2" Background="Red">

       <TextBlock Text="{Binding DataContext, ElementName=SimpleBroken}"/>

    </Grid>

    Since SimpleBroken is the target element, if its datacontext changes, then everything should be ok.  Only if the parent of simple broken data context changes will there be an issue.  Your example does not really illustrate this unless im reading it wrong.  

    Could you please clarify?

  2. David Anson says:

    leifre,

    When the left mouse button is clicked on the red/green background, the DataContext of the MainPage.xaml *UserControl* is changed, not the DataContext of the "SimpleBroken" Grid. Therefore, this triggers the bug because the DataContext change occurs on the parent of the target of the Binding.

    Sorry I wasn’t clearer about that!

  3. andrew says:

    I am confused about the context menu when used with the datagrid.  It does not change the selected row to the row that is clicked on.  How can I change the selected row, or get the data from the row that was clicked on?  Perphaps emulating the left click before entering the menu would resovle this?

    Is there any way to determine which column was clicked on, if you set up the menus for the whole grid, or do you need to define a menu for each column?

  4. David Anson says:

    andrew,

    For what it's worth, both of your questions probably involve more DataGrid experience than ContextMenu expertise. 🙂 For the purpose of selecting the row that is clicked on, you might consider handling the ContextMenu.Opened event and using that to determine the correct row and selecting that with the existing DataGrid selection APIs. For columns, defining a separate ContextMenu seems like the easiest way to get that information – but I bet there's some cleverness (possibly including the use of VisualTreeHelper and a walk of the parents) to derive the correct row and column from a single ContextMenu that's defined for the entire DataGrid. Offhand, I don't have an example of that, but I bet it would make a good blog post if you figure it out! 🙂

  5. Michael says:

    What I really want to know is when are these Silverlight binding bugs going to be fixed. This is the second one I've encountered and now I routinely, resentfully employ a "View Model Locator" to workaround that.. Binding is *central* to Silverlight programming. How can they continue putting out release after relase with such bugs?

Comments are closed.