简体   繁体   English

WPF - 复选框命令未触发

[英]WPF - Checkbox Command not firing

I am writing a WPF app using the MVVM pattern and I am having the following problem: I have bound a command to a checkbox in my UI however my event handler is not being called when the check box is clicked.我正在使用 MVVM 模式编写 WPF 应用程序,但遇到以下问题:我已将命令绑定到 UI 中的复选框,但是单击复选框时未调用我的事件处理程序。 I have used the same approach to bind other UI elements such as buttons and it seems to work alright for them.我使用了相同的方法来绑定其他 UI 元素,例如按钮,并且似乎对它们工作正常。 The relevant xaml is as follows:相关的xaml如下:

<ListBox ItemsSource="{Binding ElementsMethods}" Height="auto" x:Name="MethodsListBox">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding FormattedEM}"/>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="Started"/>
                                                        <Checkbox IsChecked="{Binding Started} Command="{Binding elementMethodCheckboxChangeCommand}"> </CheckBox>
                                                    </StackPanel>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="Finished"/>
                                                        <CheckBox IsChecked="{Binding Finished}"></CheckBox>
                                                    </StackPanel>
                                                </StackPanel>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>IsChecked="{Binding Finished}

Where elementMethodCheckboxChangeCommand is a public property of type ICommand in my viewmodel class:其中 elementMethodCheckboxChangeCommand 是我的视图模型类中 ICommand 类型的公共属性:

public ICommand elementMethodCheckboxChangeCommand { get; set; }

the concrete class used to set this property is named relay command:用于设置此属性的具体类名为中继命令:

elementMethodCheckboxChangeCommand = new RelayCommand(new Action<object>(elementMethodCheckboxChange));

where elementMethodCheckboxChange is a public void function taking a parameter of type object.其中 elementMethodCheckboxChange 是一个公共 void 函数,它采用对象类型的参数。 The implementation of the relaycommand class is as follows:中继命令类的实现如下:

class RelayCommand : ICommand
{
    private Action<object> _action;
    public RelayCommand(Action<object> action)
    {
        _action = action;
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public void Execute(object parameter)
    {
        if (parameter != null)
        {
            _action(parameter);
        }
        else
        {
            _action("Hello world");
        }
    }
    public event EventHandler CanExecuteChanged;
}

Like I said above I have used this same approach to bind to buttons in my UI and they have worked as expected, however when I click the checkbox nothing happens at all, and my event handler is not executed.就像我上面说的那样,我使用相同的方法绑定到我的 UI 中的按钮,它们按预期工作,但是当我单击复选框时,什么也没有发生,并且我的事件处理程序没有执行。

I hope someone can help me out here as this problem is starting to become really frustrating - please ask if you need any additional information.我希望有人能在这里帮助我,因为这个问题开始变得非常令人沮丧 - 请询问您是否需要任何其他信息。 Thank you all in advance :)谢谢大家 :)

You should specify a RelativeSource of the binding when you want to bind to a property of the view model inside an `ItemTemplate:当您想绑定到`ItemTemplate 中的视图模型的属性时,您应该指定绑定的RelativeSource

<CheckBox ... Command="{Binding DataContext.elementMethodCheckboxChangeCommand,
            RelativeSource={RelativeSource AncestorType=ListBox}}"/>

The default DataContext is the current item in the ItemsSource and this one has no elementMethodCheckboxChangeCommand property to bind to.默认的DataContextItemsSource的当前项目,并且这个项目没有要绑定到的elementMethodCheckboxChangeCommand属性。

Making the property static is not a very good solution.使属性静态不是一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM