【发布时间】: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