簡體   English   中英

使用 SimpleCommand 時,mahapps.metro TextBoxHelper.ButtonCommand 不觸發

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

我正在嘗試觸發 SearchMetroTextBox TextBoxHelper.ButtonCommand 事件,但我沒有任何運氣。 這是我采取的步驟。

  1. 從演示應用程序中,我構建了 MahApps.Metro.Resources.dll 並引用了它。

  2. 從mahapps.metro的Demo應用中,我復制了以下類:

    公共類 SimpleCommand : ICommand
    {
    ... 執行 ...
    }

  3. 我不想要搜索圖標,而是一個文件瀏覽圖標,所以我做了以下樣式:

     <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. 我從演示應用程序中復制了 TextBoxButtonCmdWithParameter,將其重命名為 FileBrowseCommand。

  5. 我創建了以下文本框(注意,一個是我的瀏覽文本框,一個是標准搜索文本框):

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

如果綁定到文本框按鈕的命令沒有正確的類型,就會發生這種情況。 它需要將文本框作為輸入參數。 此外,文本框必須來自 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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM