4

I have in XAML 3 menu items defined (using WPF-MDI):

<MenuItem Header="_Generic" Name="Generic" ToolTip="Generic Visual Studio designer theme" 
          Command="{Binding Path=SelectGenericTheme}"/>
<MenuItem Header="_Luna" Name="Luna" ToolTip="Blue Windows XP theme"
          Command="{Binding Path=SelectLunaTheme}"/>
<MenuItem Header="_Aero" Name="Aero" ToolTip="Windows Vista/7 theme" 
          Command="{Binding Path=SelectAeroTheme}"/>

And the definitions of the commands and current selection in the ViewModel:

    public enum ESelectedTheme
    {
        Generic,
        Luna,
        Aero
    }

    ESelectedTheme _selectedTheme;

    ICommand _selectGenericThemeCommand;
    public ICommand SelectGenericThemeCommand
    {
        get { return _selectGenericThemeCommand ?? (_selectGenericThemeCommand = new RelayCommand(param => SelectGenericTheme(), 
            param => true)); }
    }

    void SelectGenericTheme()
    {
        _selectedTheme = ESelectedTheme.Generic;
    }


    ICommand _selectLunaThemeCommand;
    public ICommand SelectLunaThemeCommand
    {
        get
        {
            return _selectLunaThemeCommand ?? (_selectLunaThemeCommand = new RelayCommand(param => SelectLunaTheme(),
                param => true));
        }
    }

    void SelectLunaTheme()
    {
        _selectedTheme = ESelectedTheme.Luna;
    }


    ICommand _selectAeroThemeCommand;
    public ICommand SelectAeroThemeCommand
    {
        get
        {
            return _selectAeroThemeCommand ?? (_selectAeroThemeCommand = new RelayCommand(param => SelectAeroTheme(),
                param => true));
        }
    }

    void SelectAeroTheme()
    {
        _selectedTheme = ESelectedTheme.Aero;
    }

I have 2 questions (hope that is allowed inside one post):

  1. I want to bind the IsChecked property in XAML to the value that is selected (_selectedTheme). I think I need to write a converter but I don't know how.
  2. I made 3 copies of ICommands (one for each theme) ... what if I would have 20 themes ... is there a way to make this code parameterized?

Thanks in advance.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • 2
    You can specify a [`CommandParameter`](http://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem.commandparameter.aspx) and you ask about how to parameterize a command? Have you always been ignoring the `param => ...` without wondering how to use that? – H.B. Feb 18 '12 at 00:47
  • With parameterize I mean to have instead of 3 commands only using 1 command (but I will check how CommandParameter works, maybe that helps). Thanks for the comment. – Michel Keijzers Feb 18 '12 at 00:48
  • With `RelayCommand(T)` it's pointless to pass in 'true' as the Predicate because it is defaulted that way to begin with. – myermian Feb 18 '12 at 01:09
  • @m-y: Depends on the implementation... – H.B. Feb 18 '12 at 01:15
  • @H.B. I guess there might be one blog out there that posts a version of `RelayCommand(T)` with only one constructor taking both parameters, but the top google searches all allow for just `Action(T)` to be passed in. – myermian Feb 18 '12 at 01:23

1 Answers1

6

It will not be necessary to parameterize the command as the binding will do everything but as noted it would be possible using CommandParameter. Here the converter will get the enum parameter.

An example:

<MenuItem Header="_Description" IsCheckable="True"
        IsChecked="{Binding Path=DisplayMode_Current,
                            Converter={StaticResource EnumToBooleanConv},
                            ConverterParameter=Description}" />
<MenuItem Header="_Web-Page" IsCheckable="True"
        IsChecked="{Binding Path=DisplayMode_Current,
                            Converter={StaticResource EnumToBooleanConv},
                            ConverterParameter=WebPage}" />

The converter can look something like this:

public class EnumToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // You could also directly pass an enum value using {x:Static},
        // then there is no need to parse
        string parameterString = parameter as string;
        if (parameterString == null)
            return DependencyProperty.UnsetValue;

        if (Enum.IsDefined(value.GetType(), value) == false)
            return DependencyProperty.UnsetValue;

        object parameterValue = Enum.Parse(value.GetType(), parameterString);

        return parameterValue.Equals(value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string parameterString = parameter as string;
        if (parameterString == null)
            return DependencyProperty.UnsetValue;

        return Enum.Parse(targetType, parameterString);
    }
}

As the XAML is still verbose (and redundant!) you could take it further by binding the ItemsSource of the parent MenuItem to the enum values and then work with the ItemTemplate and ItemContainerStyle.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I guess SelectedTheme (the property in the ViewModel) should be a dependencyproperty? If so ... I have still to learn a lot about WPF. – Michel Keijzers Feb 18 '12 at 01:35
  • 1
    @MichelKeijzers: No, never use dependency properties in a VM, you know [INPC](http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx) and [how to implement it](http://msdn.microsoft.com/en-us/library/ms229614.aspx), right? It should just be a normal property implementing that interface for binding notifications. – H.B. Feb 18 '12 at 01:37
  • Yes I'm using that ... than I have to check my code again since I don't see the enumeration value being changed after changing the menu item ... probably my fault ... It's quite hard to debug XAML code. – Michel Keijzers Feb 18 '12 at 01:40
  • 1
    [No, it's not](http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/debugging-data-bindings-in-a-wpf-or-silverlight-application.aspx). – H.B. Feb 18 '12 at 01:41
  • I think I learn a lot today(night) about XAML ... thanks for the info. – Michel Keijzers Feb 18 '12 at 01:43
  • I think there should still be some Commmand= binding in the XAML code you provided. Currently only the IsCheckable and IsChecked and Header properties are set, but what happens if I select the menu item itself (to change the value?) – Michel Keijzers Feb 18 '12 at 01:50
  • 1
    @MichelKeijzers: The property you bound to will be changed, all MenuItems - as they all bind to it - will then update and thus be unchecked. – H.B. Feb 18 '12 at 01:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7872/discussion-between-michel-keijzers-and-h-b) – Michel Keijzers Feb 18 '12 at 01:52