我仅在视图模型 class 中获得组合框选定项,但无法在视图模型 class 之外使用它,它会抛出 null

I get combo box selected item only in View Model class but can't use it out of view model class its throw null

这是我的 Xaml

  <!--Dimension Type Combobox-->
        <ComboBox x:Name="ComboBox" 
                  ItemsSource="{Binding  empList }" 
                  SelectedItem="{Binding  selectedEmployee, Mode=OneWayToSource}"
                  DisplayMemberPath="Name"
                 SelectedIndex="0"

        />


        <!--Buttons-->
        <Grid Margin="20,0,0,0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>


            <Button Grid.Column="1" Content="Ok" Margin="10,20,0,0" 
                    IsDefault="True" Command="{Binding OkCommand}" />

这是我的 ViewModel。如果我将 actions.showMessageTest(); 替换为 MessageBox.Show($"you select employee {selected.Name}");,我可以在此处使用所选项目 我可以获得消息。

class ViewModel : INotifyPropertyChanged
{
    private Actions actions;
    private wbCommand _OkCommand ;
    public wbCommand OkCommand
    {
        get { return _OkCommand; }

        set
        {
            _OkCommand = value;
            NotifyPropertyChanged(nameof(OkCommand));
        }
    }



    private ObservableCollection<employee> _empList;

    public ObservableCollection<employee> empList
    {
        get { return _empList; }
        set
        { 
            _empList = value;
            NotifyPropertyChanged(nameof(empList));
        }
    }


    private employee _selectedEmployee;

    public employee selectedEmployee
    {
        get { return _selectedEmployee; }
        set 
        {
            _selectedEmployee = value;
            NotifyPropertyChanged(nameof(selectedEmployee));
        }
    }

    public ViewModel()
    {
        OkCommand = new wbCommand();
        empList = new ObservableCollection<employee>() { new employee { Name = "mahmoud", Id = 2019 }, new employee { Name = "maged", Id = 2017 } };
        ExecuteAction();
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void ExecuteAction()
    {
       

        OkCommand.CommandAction = () =>
        {
            actions.showMessageTest();

        };
      

    }
}

这是我的 WBCommand Class 实现我命令

class wbCommand : ICommand
{

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }
    public Action CommandAction = () => { };

    public void Execute(object parameter)
    {
        CommandAction();
    }
}

这是我的操作 Class,其中包括我想通过“确定”按钮执行的操作,这里的问题是:我无法在此处获取所选项目以执行消息。

public class Actions : INotifyPropertyChanged
{
    private ViewModel vm;

    private employee _selected;
    public employee selected 
    { 
        get
        {
            return _selected ;
        }

        set
        {
            _selected = value;
            NotifyPropertyChanged(nameof(selected));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void showMessageTest()
    {
        selected = vm.selectedEmployee;

        MessageBox.Show($"you select employee {selected.Name}");

    }
}

您没有在 Actions class 中初始化 vm 字段,vm 始终为 null

例如,您可以将其作为构造函数参数传递:

public class Actions : INotifyPropertyChanged
{
    private ViewModel vm;
    public Actions(ViewModel vm)
    {
         this.vm = vm;
    }
}

您还需要初始化 ViewModel 中的 actions 字段 class:

class ViewModel : INotifyPropertyChanged
{
    private Actions actions;
    public ViewModel ()
    {
         actions = new Actions(this);
    }
}

检查所选项目是否不为空也很有意义:

public void showMessageTest()
{
    selected = vm.selectedEmployee;
    if (selected != null)
        MessageBox.Show($"you select employee {selected.Name}");
}