简体   繁体   English

使用 SimpleCommand 时,mahapps.metro TextBoxHelper.ButtonCommand 不触发

[英]mahapps.metro TextBoxHelper.ButtonCommand not firing when using SimpleCommand

I'm trying to get a SearchMetroTextBox TextBoxHelper.ButtonCommand event to fire and am not having any luck.我正在尝试触发 SearchMetroTextBox TextBoxHelper.ButtonCommand 事件,但我没有任何运气。 Here's the steps I took.这是我采取的步骤。

  1. From the Demo application, I build MahApps.Metro.Resources.dll and referenced that.从演示应用程序中,我构建了 MahApps.Metro.Resources.dll 并引用了它。

  2. From the Demo application of mahapps.metro, I copied the following class:从mahapps.metro的Demo应用中,我复制了以下类:

    public class SimpleCommand : ICommand公共类 SimpleCommand : ICommand
    { {
    ... implementation ... ... 执行 ...
    } }

  3. I didn't want the search icon, but instead a file browse icon, so I made the following style:我不想要搜索图标,而是一个文件浏览图标,所以我做了以下样式:

     <Style x:Key="BrowseMetroTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource SearchMetroTextBox}"> <Style.Triggers> <Trigger Property="Controls:TextBoxHelper.HasText" Value="False"> <Setter Property="Controls:TextBoxHelper.Watermark" Value="Select save location..." /> </Trigger> </Style.Triggers> <Setter Property="Controls:TextBoxHelper.ButtonTemplate"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid Background="{TemplateBinding Background}"> <Grid x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Opacity="0.75"> <Canvas Width="15" Height="15" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0"> <!-- x:Key="appbar_folder_ellipsis"--> <Path Width="15.7781" Height="15.7781" Stretch="Fill" Fill="{DynamicResource AccentColorBrush}" Data="F1 M 21,30.0001L 55.9999,30.0001L 55.9999,50L 21,50L 21,30.0001 ZM 52,28L 37,28C 38,25 39.4999,24.0001 39.4999,24.0001L 50.75,24C 51.3023,24 52,24.6977 52,25.25L 52,28 ZM 53.5,52C 54.8807,52 56,53.1193 56,54.5C 56,55.8807 54.8807,57 53.5,57C 52.1193,57 51,55.8807 51,54.5C 51,53.1193 52.1193,52 53.5,52 ZM 46.5,52C 47.8807,52 49,53.1193 49,54.5C 49,55.8807 47.8807,57 46.5,57C 45.1193,57 44,55.8807 44,54.5C 44,53.1193 45.1193,52 46.5,52 ZM 39.5,52C 40.8807,52 42,53.1193 42,54.5C 42,55.8807 40.8807,57 39.5,57C 38.1193,57 37,55.8807 37,54.5C 37,53.1193 38.1193,52 39.5,52 Z " /> </Canvas> </Grid> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Foreground" Value="{DynamicResource WhiteBrush}" /> <Setter Property="Background" Value="{DynamicResource AccentColorBrush}" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
  4. From the Demo application I copied the TextBoxButtonCmdWithParameter, renaming it to FileBrowseCommand.我从演示应用程序中复制了 TextBoxButtonCmdWithParameter,将其重命名为 FileBrowseCommand。

  5. I created the following TextBoxes (note, one is my browse text box and one is standard search textbox):我创建了以下文本框(注意,一个是我的浏览文本框,一个是标准搜索文本框):

     <TextBox HorizontalAlignment="Stretch" Height="23" VerticalAlignment="Top" Margin="10,26,10,0" Name="employeeLocation" Controls:TextBoxHelper.ButtonCommand="{Binding FileBrowseCommand, Mode=OneWay}" Controls:TextBoxHelper.ButtonCommandParameter="{Binding ElementName=employeeLocation, Path=Text}" Style="{StaticResource BrowseMetroTextBox}"/> <TextBox HorizontalAlignment="Stretch" Height="23" VerticalAlignment="Top" Margin="10,83,10,0" Name="adminLocation" Controls:TextBoxHelper.ButtonCommand="{Binding FileBrowseCommand, Mode=OneWay}" Controls:TextBoxHelper.ButtonCommandParameter="{Binding ElementName=adminLocation, Path=Text}" Style="{StaticResource SearchMetroTextBox}"/>

However, when I click the search or browse button, I never hit the event handler.但是,当我单击搜索或浏览按钮时,我从未点击事件处理程序。 Is there something else I'm missing?还有什么我想念的吗?

I have encountered the same problem, I solved it by changing Controls:TextBoxHelper.ButtonCommand to a nested element in my textbox:我遇到了同样的问题,我通过将Controls:TextBoxHelper.ButtonCommand更改为文本框中的嵌套元素来解决它:

<Controls:TextBoxHelper.ButtonCommand>
    <Binding Path="FileBrowseCommand" Mode="OneWay" />
</Controls:TextBoxHelper.ButtonCommand>`

This will happen, if the command that is bound to the Textbox Button does not have the right type.如果绑定到文本框按钮的命令没有正确的类型,就会发生这种情况。 It needs to have the Textbox as input parameter.它需要将文本框作为输入参数。 Also Textbox must be from System.Windows.Controls, not from System.Windows.Forms.此外,文本框必须来自 System.Windows.Controls,而不是来自 System.Windows.Forms。 The following should work when defined in your data context:在您的数据上下文中定义时,以下内容应该起作用:

public ReactiveCommand<System.Windows.Controls.TextBox, Unit> YourCommandName { get; private set; }

YourCommandName = ReactiveCommand.Create((System.Windows.Controls.TextBox textBox) =>
{
   // your command code
});

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

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