使用 Caliburn 的 TextboxHelper.ButtonCommand 绑定

Nic*_*ams 5 c# wpf mvvm caliburn.micro mahapps.metro

我在使用 caliburn 将 TextboxHelper.ButtonCommand(来自 mahapps.metro)绑定到我的视图模型时遇到问题。

目前我使用委托命令进行这项工作。

看法:

<TextBox Name="TextBoxContent"
             mui:TextboxHelper.ButtonContent="s"
             mui:TextboxHelper.ButtonCommand="{Binding DelCommand, Mode=OneWay}"
             Style="{DynamicResource ButtonCommandMuiTextBox}" />
Run Code Online (Sandbox Code Playgroud)

视图模式:

ICommand DelCommand
{
    get {return new Command();}
}

void Command() { //Handle press here }
Run Code Online (Sandbox Code Playgroud)

但是我真的很想使用 caliburn 而不是委托命令来实现这一点。我试过在文本框上使用事件触发器无济于事,就像这样......

<TextBox Name="TextBoxContent" mui:TextboxHelper.ButtonContent="s"
             Style="{DynamicResource ButtonCommandMuiTextBox}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="mui:TextboxHelper.ButtonCommand">
                <i:EventTrigger.Actions>
                    <cal:ActionMessage MethodName="Command"/>
                </i:EventTrigger.Actions>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
Run Code Online (Sandbox Code Playgroud)

有什么理由不能这样做吗?

谢谢

Rhy*_*qua 1

这是因为它不是一个事件,您可以通过将对命令的调用转换为附加事件来解决此问题,然后让 caliburn 监视该事件。

我将省略附加的事件代码,因为它很长,但可以在这里找到:Custom Attached events in WPF

就像是

public class MyControlExtension
{
    public static readonly DependencyProperty SendMahAppsCommandAsEventProperty = DependencyProperty.RegisterAttached("SendMahAppsCommandAsEvent", typeof(bool), ... etc ... );

    public static SetSendMahAppsCommandAsEvent(DependencyObject element, bool value)
    {
        if (value) 
               TextboxHelper.SetButtonCommand(element, CreateCommand(element));
        else 
               TextboxHelper.SetButtonCommand(null);
    }

    public static bool GetHeaderSizingGroup(DependencyObject element)
    {
        return (bool) element.GetValue(SendMahAppsCommandAsEventProperty);
    }

    private static ICommand CreateCommand(DependencyObject element) 
    {
          return new MyCommandThatRaisesAttachedEvent(element);
    }
}
Run Code Online (Sandbox Code Playgroud)