菜单上的WPF selectedItem或viewmodel

时间:2017-03-27 19:46:21

标签: wpf selecteditem commandparameter

我正在寻找解决一个简单问题的时间。我想在我的menuItems上使用“SelectedItem”,但经过几个小时的stackoverflow后,我发现这是不可能的。我发现了很多关于“CommandParameter”但我不明白它是如何工作的。 这就是我想要做的:我有一个菜单“background1,background2,...”。如果您在菜单中选择背景,我想将所选背景设置为背景。

这是一个学校项目,所以我们必须使用MVVM,不允许使用代码隐藏。 如何在ViewModel中“使用”Commandparameter?

这是我的mainWindow.xaml:

                <Toolbar>
                  <Menu>
                    <MenuItem Header="Background" ItemsSource="{Binding Backgrounds}">
                        <MenuItem.ItemTemplate>
                            <DataTemplate>
                                <MenuItem Header="{Binding Name}" Command="{Binding ChangeBackgroundCommand}" CommandParameter="{Binding Name}"/>
                            </DataTemplate>
                        </MenuItem.ItemTemplate>
                    </MenuItem>
                  </Menu>
                </Toolbar>

这是我的mainWindowViewModel的一部分:

public MainWindowViewModel()
    {
        //load data
        BackgroundDataService bds = new BackgroundDataService();
        Backgrounds = bds.GetBackgrounds();

        //connect command
        WijzigBackgroundCommand = new BaseCommand(WijzigBackground);
    }
private void ChangeBackground()
    {
        //I want here the name of the selected menuItem (by parameter?)
    }

}

我们使用baseCommand类(我不想改变这个类,因为我认为它是标准的):

class BaseCommand : ICommand
{
    Action actie;

    public BaseCommand(Action Actie)
    {
        actie = Actie;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        actie.Invoke();
    }
}

我经常使用stackoverflow :-)这是我的第一篇文章/问题,我希望它很清楚

1 个答案:

答案 0 :(得分:1)

试试这个:

class BaseCommand<T> : ICommand
{
    private readonly Action<T> _executeMethod = null;
    private readonly Func<T, bool> _canExecuteMethod = null;

    public BaseCommand(Action<T> executeMethod)
        : this(executeMethod, null)
    {
    }

    public BaseCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
    {
        _executeMethod = executeMethod;
        _canExecuteMethod = canExecuteMethod;
    }

    public bool CanExecute(T parameter)
    {
        if (_canExecuteMethod != null)
        {
            return _canExecuteMethod(parameter);
        }
        return true;
    }

    public void Execute(T parameter)
    {
        if (_executeMethod != null)
        {
            _executeMethod(parameter);
        }
    }

    public event EventHandler CanExecuteChanged;

    bool ICommand.CanExecute(object parameter)
    {
        if (parameter == null &&
            typeof(T).IsValueType)
        {
            return (_canExecuteMethod == null);
        }
        return CanExecute((T)parameter);
    }

    void ICommand.Execute(object parameter)
    {
        Execute((T)parameter);
     }
}

像这样使用:

public MainWindowViewModel()
{
    // ...

    // connect command
    WijzigBackgroundCommand = new BaseCommand<YourBackgroundClass>(
         (commandParam) => WijzigBackground(commandParam), 
         (commandParam) => CanWijzigBackground(commandParam));
}

private void WijzigBackground(YourBackgroundClass param)
{
    // Use 'param'
}

private bool CanWijzigBackground(YourBackgroundClass param)
{
    // Use 'param'
}
相关问题