enum in xaml in MVVVM

essamce 621 Reputation points
2021-03-24T20:41:01.053+00:00

hi

<MenuItem Header="StepOne"
    Command="{Binding CurrentStepCmd}"
    CommandParameter="{x:Static data:Steps.StepOne}"/>
<MenuItem Header="StepTwo"
    Command="{Binding CurrentStepCmd}"
    CommandParameter="{x:Static data:Steps.StepTwo}"/>

code works fine but:
Q1: i don't like my xaml to access any data from outside it's viewmodel (i don't like to include the
namespace "data" in xaml ).
Q2: is the solution of making ICommand for each enum value (Steps enum) better than using one cmd with param?

i'm using C# and wpf .Net framwork 4.7 app
any help will be appreciated,

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2021-03-25T02:32:48.643+00:00

    I make a demo to implement the data in ViewModel like below:
    Xaml code is:

       <Window.Resources>  
        <HierarchicalDataTemplate DataType="{x:Type local:UserModel}">  
            <MenuItem Header="{Binding Path=UserName}" Command="{Binding Path=ShowCommand}" CommandParameter="{Binding Path=UserAge}"/>  
        </HierarchicalDataTemplate>  
    </Window.Resources>  
      
    <Window.DataContext>  
        <local:MainViewModel></local:MainViewModel>  
    </Window.DataContext>  
    <StackPanel>  
        <Menu ItemsSource="{Binding LtUser}"></Menu>  
    </StackPanel>  
    

    The C# code is:

    using GalaSoft.MvvmLight;  
    using GalaSoft.MvvmLight.Command;  
    using System.Collections.ObjectModel;  
    using System.Windows;  
    using System.Windows.Input;  
      
    namespace CommandParameterMVVM  
    {  
        public class MainViewModel:ViewModelBase  
        {  
            private ObservableCollection<UserModel> _LtUser;  
            public ObservableCollection<UserModel> LtUser  
            {  
                get { return _LtUser; }  
                set  
                {  
                    if (_LtUser != value)  
                    {  
                        _LtUser = value;  
                        RaisePropertyChanged("LtUser");  
                    }  
                }  
            }  
      
            public MainViewModel()  
            {  
                LtUser = new ObservableCollection<UserModel>()  
                {  
                    new UserModel(){UserName="Name1",UserAge="11"},  
                    new UserModel(){UserName="Name2",UserAge="12"},  
                    new UserModel(){UserName="Name3",UserAge="13"},  
                    new UserModel(){UserName="Name4",UserAge="14"}  
                };  
            }   
        }  
      
      
        public class UserModel : ViewModelBase  
        {  
            private string _UserName;  
            public string UserName  
            {  
                get { return _UserName; }  
                set  
                {  
                    if (_UserName != value)  
                    {  
                        _UserName = value;  
                        RaisePropertyChanged("UserName");  
                    }  
                }  
            }  
      
      
            private string _UserAge;  
            public string UserAge  
            {  
                get { return _UserAge; }  
                set  
                {  
                    if (_UserAge != value)  
                    {  
                        _UserAge = value;  
                        RaisePropertyChanged("UserAge");  
                    }  
                }  
            }  
      
            public ICommand ShowCommand { get { return new RelayCommand<string>(OnShow); } }  
      
            private void OnShow(string obj)  
            {  
                MessageBox.Show("Age is:"+obj.ToString());  
            }  
        }  
    }  
    

    The result is:
    81443-2.gif

    Did it give you some help? If it does not, please give me more description to analyze.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2021-03-25T07:15:30.627+00:00

    Hi,
    you can you a simple string as Parameter:

     <MenuItem Header="StepOne"
         Command="{Binding CurrentStepCmd}"
         CommandParameter="StepOne}"/>
     <MenuItem Header="StepTwo"
         Command="{Binding CurrentStepCmd}"
         CommandParameter="StepTwo"/>
    

    and in IComand.Execute use switch (Select) with state.ToString()

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.