【问题标题】:Setting DataContext for binding RelayCommand in xaml在 xaml 中为绑定 RelayCommand 设置 DataContext
【发布时间】:2013-02-19 21:56:09
【问题描述】:

在下面的 xaml 代码中,我正在尝试绑定一个 RelayCommand ResourceButtonClick,它位于视图模型中。除此之外,我想将Resource.Id 作为参数传递给这个命令。

但是,ResourceButtonClick 没有被调用。我怀疑通过将ItemsSource 设置为Resources,我覆盖了数据上下文,即视图模型。

<UserControl ...>
    <Grid>
        <ItemsControl ItemsSource="{Binding Resources}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Tag="{Binding Id}" Content="{Binding Content}"
                    Width="300" Height="50"
                    Command="{Binding ResourceButtonClick}"
                    CommandParameter="{Binding Id}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

这是视图模型中的RelayCommand

public RelayCommand<int> ResourceButtonClick { get; private set; }

视图模型的构造函数:

public ResourcesViewModel()
{
    this.ResourceButtonClick = 
        new RelayCommand<int>((e) => this.OnResourceButtonClick(e));
}

视图模型中的方法:

private void OnResourceButtonClick(int suggestionId)
{
...
}

我有两个问题:第一,如何调用ResourceButtonClick 命令。其次,如何将Resource.Id 作为参数传递给该命令。

任何建议将不胜感激。

【问题讨论】:

  • 你能展示初始化ResourceButtonClick命令的代码吗?

标签: silverlight xaml mvvm mvvm-light relaycommand


【解决方案1】:

也许你可以展示你完整的 ViewModel 类?假设 ResourceButtonClick 命令位于同时包含集合 Resources 的 ViewModel 上,您正试图访问错误对象上的命令(在 Resources,而不是保存 Resources 集合和命令的 ViewModel)。

因此,您必须访问“其他”DataContext 上的命令,这是 ItemsControl 的 DataContext 而不是它的项目。最简单的方法是使用绑定的 ElementName 属性:

<UserControl ...>
    <Grid>
        <ItemsControl ItemsSource="{Binding Resources}" Name="ResourcesItemsControl">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Tag="{Binding Id}" Content="{Binding Content}"
                    Width="300" Height="50"
                    Command="{Binding DataContext.ResourceButtonClick, ElementName=ResourcesItemsControl}"
                    CommandParameter="{Binding Id}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

也许这可以解决问题,否则请告诉我并提供更多细节。

我通常将整个项目作为命令参数传递,而不是 ID。这不需要任何费用,您也不必将 ID 翻译回物品。但这取决于你的情况。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我以这样的方式解决了这个问题: 在 View.xaml 中

    1) 我为我的 ListView 添加了一个属性 SelectedItem:

    <ListView Name="MyList" ItemsSource="{Binding MyList}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" >
    

    2) 我向按钮添加了一个 Command 属性:

    在视图模型中: 3)我添加了一个命令处理程序:

    MyCommand = new RelayCommand(MyMethod);
    

    4) 我添加了 MyMethod 方法,它从 MySelectedItem 属性中获取值:

    private void MyMethod ()
           {
                MyType mt = MySelectedItem;
                //now you have access to all properties of your item via mt object
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多