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

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

Below 2 type of controls:

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

        <ItemsControl ... ItemsSource="{Binding MenuList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton ... prism:Click.Command="{Binding MenuClickedCommand}" >
                        <RadioButton.Template>
                            <ControlTemplate>
                                <Button prism:Click.Command="{Binding MenuClickedCommand"/>
                            </ControlTemplate>
                        </RadioButton.Template>
                    </RadioButton>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
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 5:14 PM
Edited Nov 4, 2011 at 5:16 PM

Give a name to your view



<UserControl x:Class="Modules..."             
             x:Name="dialogWindow"
             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}"
                                                        commands:Clicked.CommandParameter="{Binding}">
                                            <Button.Content>
                                                <Image Source="/Common.Content;component/Images/Remove_16x16.png" Height="16" Width="16"/>
                                            </Button.Content>
                                        </Button>


Developer
Nov 4, 2011 at 5:44 PM

Hi,

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
http://blogs.southworks.net/dcherubini

Nov 8, 2011 at 3:25 AM
Edited Nov 8, 2011 at 3: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

http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx