Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在WPF和MVVM中从StackPanel内的组合框触发事件_Wpf_Mvvm Light - Fatal编程技术网

如何在WPF和MVVM中从StackPanel内的组合框触发事件

如何在WPF和MVVM中从StackPanel内的组合框触发事件,wpf,mvvm-light,Wpf,Mvvm Light,我在StackPanel里面有一个组合框。我正在使用MVVM,并尝试将“GotFocus”事件命令绑定到ViewModel中的命令,但当我单击“ComboBox”时,它不起作用,它不会在ViewModel中调用命令,但如果我将“ComboBox”从“StackPanel”中移出,它会正常工作 How can I fire event from 'CombBox' inside 'StackPanel' in MVVM? <StackPanel x:Name="StackPanel"

我在StackPanel里面有一个组合框。我正在使用MVVM,并尝试将“GotFocus”事件命令绑定到ViewModel中的命令,但当我单击“ComboBox”时,它不起作用,它不会在ViewModel中调用命令,但如果我将“ComboBox”从“StackPanel”中移出,它会正常工作

How can I fire event from 'CombBox' inside 'StackPanel' in MVVM?

  <StackPanel x:Name="StackPanel" Grid.Column="2" Grid.Row="6">
    <ComboBox x:Name="ComboBox" ItemsSource="{Binding Values}">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="GotFocus">
          <cmd:EventToCommand Command="{Binding Path=GotFocusCommand}"/>
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </ComboBox>
  </StackPanel>

ViewModel's code is:

public ViewModelCommand GotFocusCommand { get; set; } 

////将标记从EventToCommand更改为InvokeCommandAction

<StackPanel x:Name="StackPanel" Grid.Column="2" Grid.Row="6">
<ComboBox x:Name="ComboBox" ItemsSource="{Binding Values}">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="GotFocus">
      <cmd:InvokeCommandAction="{Binding Path=GotFocusCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ComboBox>
////RelayCommandClass.cs:

public class RelayCommand : ICommand
{
private Action _handler;
public RelayCommand(Action handler)
{
    _handler = handler;
}

private bool _isEnabled;
public bool IsEnabled
{
    get { return _isEnabled; }
    set
    {
        if (value != _isEnabled)
        {
            _isEnabled = value;
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
}

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

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
    _handler();
}
}
////最后,您可以在视图模型中创建事件:

  private ICommand _GotFocusCommand;
    public ICommand GotFocusCommand
    {
        get
        {
            if (_GotFocusCommand == null)
            {
                _GotFocusCommand =
                    new RelayCommand(
                        param => GotFocusCommand_Executed(),
                        GotFocusCommand_CanExecute
                     );
            }

            return _GotFocusCommand;
        }
    }
private void GotFocusCommand_Executed()
    {
        //DoSomething here
    }

private bool GotFocusCommand_CanExecute()
    {
        return true;
    }

什么是ViewModelCommand?这是您的一些自定义类吗?我将首先尝试在Combobox上为GotFocus事件绑定一个普通的处理程序,并查看它是否被调用。例如,如果您的页面只有一个组合框,则可以在分配DataContext之前选择该组合框,然后不触发事件。在我的viewModel中,有一个命令我使用组合框EventToCommand绑定,即public ICommand和GotFocusCommand{get{return\u gotFocusCommand???\u gotFocusCommand=new relaycommandmethodname要定义,=>\u isLoaded;}我正在使用“GalaSoft.MvvmLight.Command”,因此我只能访问“EventToCommand”。请告诉我“InvokeCommandAction”属于哪个命名空间。Thanksxmlns:I=来自Microsoft.Expression.Interactions.dll