c# - 使用 Caliburn 的 TextboxHelper.ButtonCommand 绑定(bind)

标签 c# wpf mvvm caliburn.micro mahapps.metro

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

目前我使用委托(delegate)命令来完成这项工作。

查看:

<TextBox Name="TextBoxContent"
             mui:TextboxHelper.ButtonContent="s"
             mui:TextboxHelper.ButtonCommand="{Binding DelCommand, Mode=OneWay}"
             Style="{DynamicResource ButtonCommandMuiTextBox}" />

View 模式:

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

void Command() { //Handle press here }

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

<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>

为什么不能这样做?

谢谢

最佳答案

我创建了一个附加属性,将 Caliburn 消息附加到 MahApps 文本框按钮。

public static class MahappsHelper
{
    /// <summary>
    /// Attach Caliburn cal:Message.Attach for the Mahapps TextBoxHelper.Button
    /// </summary>
    public static readonly DependencyProperty ButtonMessageProperty =
        DependencyProperty.RegisterAttached("ButtonMessage", typeof(string), typeof(MahappsHelper), new PropertyMetadata(null, ButtonMessageChanged));

    public static string GetButtonMessage(DependencyObject obj)
    {
        return (string)obj.GetValue(ButtonMessageProperty);
    }

    public static void SetButtonMessage(DependencyObject obj, string value)
    {
        obj.SetValue(ButtonMessageProperty, value);
    }

    private static void ButtonMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement textBox = d as TextBox;
        if (d == null)
            textBox = d as PasswordBox;
        if (d == null)
            textBox = d as ComboBox;
        if (textBox == null)
            throw new ArgumentException("ButtonMessage does not work with control " + d.GetType());

        textBox.Loaded -= ButtonMessageTextBox_Loaded;

        Button button = GetTextBoxButton(textBox);
        if (button != null)
        {
            SetButtonMessage(textBox, button);
            return;
        }

        // cannot get button, try it in the Loaded event
        textBox.Loaded += ButtonMessageTextBox_Loaded;
    }

    private static Button GetTextBoxButton(FrameworkElement textBox)
    {
        return TreeHelper.FindChild<Button>(textBox, "PART_ClearText");
    }

    private static void ButtonMessageTextBox_Loaded(object sender, RoutedEventArgs e)
    {
        FrameworkElement textBox = (FrameworkElement)sender;
        textBox.Loaded -= ButtonMessageTextBox_Loaded;

        Button button = GetTextBoxButton(textBox);
        SetButtonMessage(textBox, button);
    }

    /// <summary>
    /// Set Caliburn Message to the TextBox button.
    /// </summary>
    private static void SetButtonMessage(FrameworkElement textBox, Button button)
    {
        if (button == null)
            return;

        string message = GetButtonMessage(textBox);
        Caliburn.Micro.Message.SetAttach(button, message);
    }
}

和用法:

    <TextBox x:Name="SearchBox" mahapps:TextBoxHelper.Watermark="Search ..."
             cal:Message.Attach="[KeyDown] = [Search(SearchBox.Text, $eventArgs)]"
             my:MahappsHelper.ButtonMessage="Search(SearchBox.Text)" />

关于c# - 使用 Caliburn 的 TextboxHelper.ButtonCommand 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041875/

相关文章:

c# - 在并行数据处理中使用什么类型的队列 - C# - .NET 4

wpf - 如何在WPF应用程序中以 Prism 方式向应用程序添加菜单?

c# - 在 ComboBox 中显示缩短的文件路径,但在 ComboBox 下拉列表中显示完整路径

c# - 让 Caliburn.Micro 使用 Windsor 在单独的程序集中绑定(bind)我的 View 和 VM

c# - 匿名函数转换

c# - 如何将大于 1e-8 的浮点值强制为 0

c# - Windows Phone MVVM + Prism --- 将事件转换为命令

c# - 谁拥有 Message Box、View 或 ViewModel?

c# - 如何从 Windows 服务访问 localDB

c# - 在 WPF 工具箱中找不到我的 winforms 控件