WPF ReactiveUI将ControlTemplate上的按钮绑定到ReactiveCommand

时间:2018-06-29 08:32:44

标签: c# wpf xaml mvvm reactiveui

使用ReactiveUI,我想将ControlTemplate上的按钮绑定到ViewModel上的简单命令

MyViewModel.cs 中:

    public ReactiveCommand<Unit, Unit> MyBCommand;

        // somewhere in MyViewModel ctor...
        MyBCommand = ReactiveCommand.Create(() =>
        {
            // TODO some stuff...
        });

MyView.xaml 中:

<UserControl.Resources>
   <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Button Name="MyBButton" .../>

位于 MyView.xaml.cs 后面的代码中:

        // somewhere in MyView ctor...
        this.WhenActivated(d =>
        {
            d(this.BindCommand(ViewModel, 
                vm => vm.MyBCommand, 
                v => v.MyBButton)); // <<==== !! not reachable!! problem!!

v.MyBButton后面的代码无法解析。
如何绑定此按钮?

基于https://reactiveui.net/docs/handbook/commands/an-example

0 个答案:

没有答案