我试图让SearchMetroTextBox发送TextBoxHelper.ButtonCommand事件并且没有任何运气。这是我采取的步骤。
从Demo应用程序中,我构建了MahApps.Metro.Resources.dll并引用了它。
从mahapps.metro的Demo应用程序中,我复制了以下类:
public class 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。
我创建了以下TextBoxes(注意,一个是我的浏览文本框,一个是标准搜索文本框):
<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}"/>
然而,当我点击搜索或浏览按钮时,我从未点击过事件处理程序。还有其他我想念的东西吗?
答案 0 :(得分:0)
我遇到了同样的问题,我通过将Controls:TextBoxHelper.ButtonCommand
更改为文本框中的嵌套元素解决了这个问题:
<Controls:TextBoxHelper.ButtonCommand>
<Binding Path="FileBrowseCommand" Mode="OneWay" />
</Controls:TextBoxHelper.ButtonCommand>`
答案 1 :(得分:0)
如果绑定到文本框按钮的命令没有正确的类型,就会发生这种情况。它需要将文本框作为输入参数。此外,文本框必须来自 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
});