mahapps.metro TextBoxHelper.ButtonCommand在使用SimpleCommand时未激
本文关键字:SimpleCommand metro TextBoxHelper ButtonCommand mahapps | 更新日期: 2023-09-27 18:24:16
我正试图启动SearchMetroTextBox TextBoxHelper.ButtonCommand事件,但没有成功。以下是我采取的步骤。
-
在Demo应用程序中,我构建了MahApps.Metro.Resources.dll并引用了它。
-
从mahabs.metro的Demo应用程序中,我复制了以下类:
公共类SimpleCommand:ICommand
{
…实施
} -
我不想要搜索图标,而是一个文件浏览图标,所以我制作了以下样式:
<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 Z M 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 Z M 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 Z M 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 Z M 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>
-
从Demo应用程序中,我复制了TextBoxButtonCmdWithParameter,并将其重命名为FileBrowseCommand。
-
我创建了以下文本框(注意,一个是我的浏览文本框,另一个是标准搜索文本框):
<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}"/>
但是,当我单击搜索或浏览按钮时,我从未命中事件处理程序。我还缺少什么吗?
我遇到了同样的问题,我通过将Controls:TextBoxHelper.ButtonCommand
更改为文本框中的嵌套元素来解决它:
<Controls:TextBoxHelper.ButtonCommand>
<Binding Path="FileBrowseCommand" Mode="OneWay" />
</Controls:TextBoxHelper.ButtonCommand>`
如果绑定到文本框按钮的命令没有正确的类型,就会发生这种情况。它需要将Textbox作为输入参数。此外,Textbox必须来自System.Windows.Controls,而不是System.Windows.Forms。当在数据上下文中定义时,以下内容应该有效:
public ReactiveCommand<System.Windows.Controls.TextBox, Unit> YourCommandName { get; private set; }
YourCommandName = ReactiveCommand.Create((System.Windows.Controls.TextBox textBox) =>
{
// your command code
});