【问题标题】:TextboxHelper.ButtonCommand Binding using Caliburn使用 Caliburn 绑定 TextboxHelper.ButtonCommand
【发布时间】:2014-01-29 06:46:10
【问题描述】:

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

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

查看:

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

查看模式:

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

void Command() { //Handle press here }

但是我真的很想使用 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>

这有什么不能做到的原因吗?

谢谢

【问题讨论】:

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


    【解决方案1】:

    我创建了一个附加属性,将 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)" />
    

    【讨论】:

      【解决方案2】:

      这是因为它不是事件,您可以通过将对命令的调用转换为附加事件来解决此问题,然后让 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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 2018-07-29
        • 1970-01-01
        • 2014-01-11
        相关资源
        最近更新 更多