Resolved: Commanding, how to specifically bind to ViewModel at View?

Topics: Prism v4 - Silverlight 4
Nov 4, 2011 at 10:54 AM

Below 2 type of controls:

        <Button prism:Click.Command="{Binding MenuClickedCommand}"/>

        <ItemsControl ... ItemsSource="{Binding MenuList}">
                    <RadioButton ... prism:Click.Command="{Binding MenuClickedCommand}" >
                                <Button prism:Click.Command="{Binding MenuClickedCommand"/>
the 1st button ClickCommand will worked.
Both of the Radio and Button within the ItemsControl, they won't.
I guess need to specify the Source, to point to the View(itself).ViewModel(property), how to code/do that?
addtionally the Radio click (normal click event) doesn't work at all.
Nov 4, 2011 at 4:14 PM
Edited Nov 4, 2011 at 4:16 PM

Give a name to your view

<UserControl x:Class="Modules..."             
             Height="446" Width="634">
Use ElementName in you binding. Here is the example:
                                        <Button Style="{StaticResource TransparentImageButtonStyle}" Grid.Column="1" Margin="6,0,0,0" VerticalAlignment="Center"
                                                        ToolTip="..." Cursor="Hand"
                                                        commands:Clicked.Command="{Binding ElementName=dialogWindow,Path=DataContext.DetachTagCommand}"
                                                <Image Source="/Common.Content;component/Images/Remove_16x16.png" Height="16" Width="16"/>

Nov 4, 2011 at 4:44 PM


Based on my understanding, the reason of why the aforementioned command bindings are not working properly is because the bindings are defined inside a DataTemplate.

A possible approach to solve this is to use the ObservableObject<T> class (included in the Prism library) useful to create binding from a DataTemplate to the ViewModel of your View. In the following blog post by Guido Maliandi you can find a detailed explanation about how to use this class and also a sample application which you can check in order to have a better understanding of how to use the ObservableObject<T> class in your code.

I hope you find it useful,

Damian Cherubini

Nov 8, 2011 at 2:25 AM
Edited Nov 8, 2011 at 2:28 AM

tillias work, and its simple.

and thx to DCherubini too, but I prefer reference through the ViewModel/View.DataContext.

though I adopted another implementation