【问题标题】:How to pass a UserControl with CommandParameter?如何使用 CommandParameter 传递 UserControl?
【发布时间】:2012-05-13 09:24:44
【问题描述】:

我使用了 DelegateCommand,我想用 Command 传递 UserControl。

#region OpenViewCommand
private DelegateCommand<UserControl> _openViewCommand;
public ICommand OpenViewCommand
{
    get
    {
        if (_openViewCommand == null)
            _openViewCommand = new DelegateCommand<UserControl>(new Action<UserControl>(OpenView));
        return _openViewCommand;
    }
}

public void OpenView(UserControl ViewName)
{
    UserControl ctrl = (UserControl)ViewName;
    JIMS.Controls.Message.Show(ViewName.ToString());
}
#endregion

XAML 中的命令

<Button Name="btnStockGroups" Command="{Binding OpenViewCommand}" CommandParameter="JIMS.View.Stock.StockGroups">stock group</Button>

【问题讨论】:

  • 为什么需要访问 ViewModel 中的控件?一般的 MVVM 方法是将控件绑定到 ViewModel 并从绑定的属性中读取数据。
  • OpenView 命令是否旨在使特定类型的 UserControl 被实例化并显示给用户?

标签: c# wpf xaml binding view


【解决方案1】:

如果你给你的 UserControl 一个 x:Name(例如“MyView”),你应该能够做这样的事情:

<Button Name="btnStockGroups" 
        Command="{Binding OpenViewCommand}" 
        CommandParameter="{Binding ElementName=MyView}">

【讨论】:

  • 该元素不在窗口中。它是一个 UserControl 文件
  • 不给元素命名怎么办?
猜你喜欢
  • 2022-01-26
  • 1970-01-01
  • 2012-09-04
  • 2016-04-27
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
相关资源
最近更新 更多