使用viewmodel控制按钮点击

发布于 2024-12-02 13:35:00 字数 519 浏览 1 评论 0原文

我试图通过视图模型(MVVM)控制button_click。 命令

<Button x:Name="GetData" Content="Get Data" Margin="8,8,223,0"   VerticalAlignment="Top">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <cmd:EventToCommand Command="GetData_Click"></cmd:EventToCommand>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

我使用以下代码创建了单击事件应返回人员列表的

。谁能为我提供“我们如何在视图模型中创建命令?”的解决方案?请提供带有示例代码的答案..

提前感谢..

i m trying to control the button_click through the viewmodel(MVVM). I've used following code to create the command

<Button x:Name="GetData" Content="Get Data" Margin="8,8,223,0"   VerticalAlignment="Top">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <cmd:EventToCommand Command="GetData_Click"></cmd:EventToCommand>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

the clicked event should return a list of persons.

Can anyone provide me solution of "How can we create a command in viewmodel?" Please provide answer with a sample code..

Thanx in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静待花开 2024-12-09 13:35:00

如果您使用的是 MVVM light,则可以执行类似于以下操作的操作:

private RelayCommand _getData_Click = null;
public RelayCommand GetData_Click
{
    get
    {
        return _getData_Click ?? _getData_Click = new RelayCommand(
            () =>
            {
                // Get Person List
            });
    }
}

MVVM Lights codeplex 页面<上有一个示例。 /a>

更新
您可能会收到错误,因为您需要使用 DataBinding 来设置命令,尝试

<Button x:Name="GetData" Content="Get Data" Margin="8,8,223,0" VerticalAlignment="Top" Command="{Binding Path=GetData_Click}"/>

或将您的 EventToCommand 声明更改为:

<cmd:EventToCommand Command={Binding GetDataCommand} />

如果您已将 ViewModel 设置为 DataContext,那么这应该可以工作。

If you're using MVVM light you can do something similar to the following:

private RelayCommand _getData_Click = null;
public RelayCommand GetData_Click
{
    get
    {
        return _getData_Click ?? _getData_Click = new RelayCommand(
            () =>
            {
                // Get Person List
            });
    }
}

There is an example of this on MVVM Lights codeplex page

Update
You're probably getting the error because you'll need to use DataBinding to setup the command, try

<Button x:Name="GetData" Content="Get Data" Margin="8,8,223,0" VerticalAlignment="Top" Command="{Binding Path=GetData_Click}"/>

or changing your EventToCommand declaration to:

<cmd:EventToCommand Command={Binding GetDataCommand} />

That should work if you've set your ViewModel as the DataContext.

猥琐帝 2024-12-09 13:35:00

如果您有一个 ViewModel 作为视图后面的 DataContext,并且您的 ViewModel 上有一个名为 GetData_Click 的命令,那么您可以执行以下操作:

  <Button Command="{Binding Path=GetData_Click"}>

但是,我有一种感觉 GetData_Click 是一个函数(根据它的名称),所以在您的 ViewModel 上,您需要定义一个命令属性,例如:

  public ICommand GetDataCommand 
  {
       get {  return (_getDataCommand  = _getDataCommand ?? new DelegateCommand(GetData));  }
  } 

其中 GetData 是一个函数。简单的 DelegateCommand 实现可以在此处找到。

If you have a ViewModel as DataContext behind the view, and you have a command on your ViewModel called GetData_Click then the following is how you do this:

  <Button Command="{Binding Path=GetData_Click"}>

However, I've got a feeling GetData_Click is a function (by the name of it) so on your ViewModel you need to define a command property, e.g.:

  public ICommand GetDataCommand 
  {
       get {  return (_getDataCommand  = _getDataCommand ?? new DelegateCommand(GetData));  }
  } 

where GetData is a function. The simple DelegateCommand implementation can be found here.

要走干脆点 2024-12-09 13:35:00

在视图模型中,您可以编写命令(也使用 MVVM Light Toolkit)

RelayCommand getDataCommand;
public RelayCommand GetDataCommand
{
    get
    {
        if (getDataCommand == null)
        {
            getDataCommand = new RelayCommand(() =>
            {
                //Here comes the command implementation
            });
        }
        return getDataCommand;
    }
}

在视图中,将 EventToCommand 更改为:

<cmd:EventToCommand Command={Binding GetDataCommand} />

In the viewmodel you can write your command (using also MVVM Light Toolkit)

RelayCommand getDataCommand;
public RelayCommand GetDataCommand
{
    get
    {
        if (getDataCommand == null)
        {
            getDataCommand = new RelayCommand(() =>
            {
                //Here comes the command implementation
            });
        }
        return getDataCommand;
    }
}

In the view, change the EventToCommand to this:

<cmd:EventToCommand Command={Binding GetDataCommand} />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文