c# - 如何在 ViewModel 中捕获动态按钮点击

标签 c# wpf viewmodel

我通过绑定(bind) ObservableCollection 创建了一堆按钮。

在我的 ViewModel 中,我想捕获点击事件。

对于我常用的按钮:

RelayCommand launchCommand;

public ICommand LaunchCommand{
    get{
        if (launchCommand == null){
            launchCommand = new RelayCommand(LaunchCommandExecute, CanLaunchCommandExecute);
        }
        return launchCommand;
    }
}

private void LaunchCommandExecute(object parameter){
    //Do something to recognize the button.
    //Could use ObservableCollection<Module> module_objects
    //to match, if I could get the buttons content or name
}

private bool CanLaunchCommandExecute(object parameter){
    return true;
}

在 LaunchCommandExecute 中,我提出了一些想法。我会对持有什么对象参数感兴趣?对我有用吗?

该按钮具有以下绑定(bind),我可以使用它们来匹配:

<Button Tag="{Binding ModuleName}" Content="{Binding ModuleAbbreviation}" Command="{Binding LaunchCommand}" IsEnabled="{Binding ModuleDisabled}" Style="{DynamicResource LauncherButton}" Background="{Binding ModuleColor}" />

有人知道怎么做吗?

[编辑]这是在接受下面的答案之后

我发现 LaunchCommand 没有触发。我想知道下面的代码是否有冲突?

<UserControl.DataContext>
    <viewmodel:LauncherViewModel />
</UserControl.DataContext>
<Grid >
    <ItemsControl ItemsSource="{Binding Source={x:Static m:ModuleKey._module_objects}}" >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <my:AlignableWrapPanel HorizontalAlignment="Stretch" Name="alignableWrapPanel1" VerticalAlignment="Center" HorizontalContentAlignment="Center" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="10">
                    <Button Content="{Binding ModuleAbbreviation}" Command="{Binding LaunchCommand}" CommandParameter="{Binding ModuleName}" IsEnabled="{Binding ModuleDisabled}" Style="{DynamicResource LauncherButton}" Background="{Binding ModuleColor}" FontSize="32" FontFamily="Tahoma" Width="130" Height="100" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

[EDIT Answer] 没想到我正在尝试做什么,发现命令看不到正确的 DataContext。添加以下排序:

Command="{Binding DataContext.LaunchCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"

最佳答案

参数CommandParameter设置。在这种情况下,您所要做的就是将它绑定(bind)到“ModuleName”:

<Button Command="{Binding LaunchCommand}" CommandParameter="{Binding ModuleName}" ...

使用强制转换来获取它——假设它是一个字符串:

private void LaunchCommandExecute(object parameter){
    string moduleName = parameter as string;
    // ...
}

(请注意,您还可以使用 {Binding RelativeSource={RelativeSource Self},Path=Tag}CommandParameter 设置为按钮的标签或内容,但那样会在这种情况下是一种迂回的方法。)

关于c# - 如何在 ViewModel 中捕获动态按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357410/

相关文章:

javascript - NativeScript - viewModel.foo 和 viewModel.get ("foo"之间有什么区别)?

c# - 创建 ViewModel 类

javascript - Web API 发布参数

c# - 如何使用 Binding 在 TextBlock 中实现 NullText?

C# MySQL 参数化更新不返回任何内容

c# - HttpClient PostAsync 和 SendAsync 之间的区别

wpf - WPF 中 .NET (Rx) 的响应式扩展 - MVVM

asp.net-mvc-3 - 摘要/首页上的 ViewModel 与部分 View

c# - 在运行时更改语言的正确方法

c# - 解析来自总线的命令