MVVM, ContextMenus and binding to ViewModel defined Command

Posted by Simon Fox on Stack Overflow See other posts from Stack Overflow or by Simon Fox
Published on 2010-03-18T02:05:21Z Indexed on 2010/03/18 2:11 UTC
Read the original article Hit count: 443

Filed under:
|
|
|

Hi

I am having problems with the binding of a ContextMenu command to an ICommand property in my ViewModel. The binding seems to be attaching fine...i.e when I inspect the value of the ICommand property it is bound to an instance of RelayCommand. The CanExecute delegate does get invoked, however when I open the context menu and select an item the Execute delegate does not get invoked.

Heres my View (which is defined as the DataTemplate to use for instances of the following ViewModel in a resource dictionary):

<UserControl x:Class="SmartSystems.DragDropProto.ProductLinkView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Proto">

    <UserControl.Resources>
        <local:CenteringConverter x:Key="centeringConvertor">
                                     </local:CenteringConverter>
    </UserControl.Resources>

    <UserControl.ContextMenu>
        <ContextMenu>
            <MenuItem Command="{Binding ChangeColor}">Change Color</MenuItem>
        </ContextMenu>
    </UserControl.ContextMenu>

    <Canvas>
        <Ellipse Width="5" Height="5" >
            <Ellipse.Fill>
                <SolidColorBrush Color="{Binding LinkColor}"></SolidColorBrush>
            </Ellipse.Fill>
            <Ellipse.RenderTransform>
                <TranslateTransform X="{Binding EndpointOneXPos, 
                                       Converter={StaticResource centeringConvertor}}"
                                    Y="{Binding EndpointOneYPos,
                                       Converter={StaticResource centeringConvertor}}"/>
            </Ellipse.RenderTransform>
        </Ellipse>
        <Line X1="{Binding Path=EndpointOneXPos}" Y1="{Binding Path=EndpointOneYPos}"
              X2="{Binding Path=EndpointTwoXPos}" Y2="{Binding Path=EndpointTwoYPos}">
            <Line.Stroke>
                <SolidColorBrush Color="{Binding LinkColor}"></SolidColorBrush>
            </Line.Stroke>
        </Line>
        <Ellipse Width="5" Height="5" >
            <Ellipse.Fill>
                <SolidColorBrush Color="{Binding LinkColor}"></SolidColorBrush>
            </Ellipse.Fill>
            <Ellipse.RenderTransform>
                <TranslateTransform X="{Binding EndpointTwoXPos,
                                       Converter={StaticResource centeringConvertor}}"
                                    Y="{Binding EndpointTwoYPos,
                                       Converter={StaticResource centeringConvertor}}"/>
            </Ellipse.RenderTransform>
        </Ellipse>
    </Canvas>        
</UserControl>

and ViewModel (with uneccessary implementation details removed):

class ProductLinkViewModel : BaseViewModel {

public ICommand ChangeColor
{
    get;
    private set;
}

public Color LinkColor
{
    get;
    private set;
}

public ProductLinkViewModel(....)
{
    ...

    ChangeColor = new RelayCommand(ChangeColorAction);
    LinkColor = Colors.Blue;
}

private void ChangeColorAction(object param)
{
    LinkColor = LinkColor == Colors.Blue ? Colors.Red : Colors.Blue;

    OnPropertyChanged("LinkColor");
}

}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about mvvm