wpf Mahapps.Metro:自定义图标文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22314763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:10:48  来源:igfitidea点击:

Mahapps.Metro: Custom Icon Textbox

c#wpfxamlmahapps.metro

提问by joshua-anderson

I would like to add a icon to the mahapps.metro textbox, much like the clear button for the textbox. The mahapps.metro demo app has this feature, and even after looking at the source code don't understand how to implement it. From TextExamples.xamlin the Metro Demo app the code bellow code creates a custom icon and binds it to a event. However, I can't implement it myslef in my own code. I can't tell at all where it is getting the custom icon from. Thanks from

我想向 mahapps.metro 文本框添加一个图标,就像文本框的清除按钮一样。mahapps.metro 演示应用程序有此功能,即使查看源代码也不明白如何实现它。从TextExamples.xaml在地铁演示应用娄代码的代码创建一个自定义图标并绑定到一个事件。但是,我无法在自己的代码中自行实现它。我完全不知道它从哪里获取自定义图标。感谢来自

<TextBox Margin="0, 10, 0, 0"
                         Controls:TextboxHelper.Watermark="Custom icon style"
                         Controls:TextboxHelper.ButtonContent="s"
                         Controls:TextboxHelper.ButtonCommand="{Binding TextBoxButtonCmd,  Mode=OneWay}"
                         Style="{DynamicResource MetroButtonTextBox}" />

回答by Bojin Li

The documentation in the source code does not mention it, but it appears the Controls:TextboxHelper.ButtonContentProperty is what controls the custom icons. You have to set the ButtonContentProperty to the font character you want in this font called Marlett Characters

源代码中的文档没有提到它,但它似乎Controls:TextboxHelper.ButtonContent是控制自定义图标的属性。您必须ButtonContent在名为 Marlett Characters 的字体中将属性设置为所需的字体字符

http://www.reactos.org/wiki/Marlett_Characters

http://www.reactos.org/wiki/Marlett_Characters

I found this answer in the project's GitHub site https://github.com/MahApps/MahApps.Metro/issues/849

我在项目的 GitHub 站点https://github.com/MahApps/MahApps.Metro/issues/849 中找到了这个答案

回答by punker76

here is the example how the search TextBoxworks (and should work with your custom vector icon or something else too)

这是搜索如何TextBox工作的示例(并且应该与您的自定义矢量图标或其他东西一起使用)

<Style TargetType="{x:Type TextBox}"
       x:Key="SearchMetroTextBox"
       BasedOn="{StaticResource MetroButtonTextBox}">
    <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 48,0L 48,48L 0,48L 0,0">
                            <!-- x:Key="appbar_magnify"-->
                            <Path Width="15.7781"
                                  Height="15.7781"
                                  Stretch="Fill"
                                  Fill="{TemplateBinding Foreground}"
                                  Data="F1 M 14.8076,31.1139L 20.0677,25.9957C 19.3886,24.8199 19.25,23.4554 19.25,22C 19.25,17.5817 22.5817,14 27,14C 31.4183,14 35,17.5817 35,22C 35,26.4183 31.4183,29.75 27,29.75C 25.6193,29.75 24.3204,29.6502 23.1868,29.0345L 17.8861,34.1924C 17.105,34.9734 15.5887,34.9734 14.8076,34.1924C 14.0266,33.4113 14.0266,31.895 14.8076,31.1139 Z M 27,17C 24.2386,17 22,19.2386 22,22C 22,24.7614 24.2386,27 27,27C 29.7614,27 32,24.7614 32,22C 32,19.2386 29.7614,17 27,17 Z " />
                        </Canvas>
                    </Grid>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver"
                             Value="True">
                        <Setter TargetName="contentPresenter"
                                Property="Opacity"
                                Value="1" />
                    </Trigger>
                    <Trigger Property="IsMouseOver"
                             Value="False">
                        <Setter TargetName="contentPresenter"
                                Property="Opacity"
                                Value=".5" />
                    </Trigger>
                    <Trigger Property="IsEnabled"
                             Value="False">
                        <Setter Property="Foreground"
                                Value="#ADADAD" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

hope that helps

希望有帮助

回答by har07

Not a complete answer, just direction where to check for implementation of Icon in TextBox by MahApps.Metro.

不是一个完整的答案,只是指示在何处通过 MahApps.Metro 检查 TextBox 中 Icon 的实现。

First thing I will do is to check Controls.TextBox.xamlfile. There is where MetroButtonTextBoxstyle implemented :

我要做的第一件事是检查Controls.TextBox.xaml文件。有MetroButtonTextBox风格实现的地方:

<Style TargetType="{x:Type TextBox}"
       BasedOn="{StaticResource MetroTextBox}"
       x:Key="MetroButtonTextBox">
    <Setter Property="Controls:TextboxHelper.ButtonTemplate"
            Value="{DynamicResource ChromelessButtonTemplate}" />
    <!-- change SnapsToDevicePixels to True to view a better border and validation error -->
    <Setter Property="Template">
        <Setter.Value>
            ......
            ......
        </Setter.Value>
    </Setter>
</Style>

And I guess (haven't dug too deep on this), the Icon is buttonpart of the TextBox. Therefore you may need to go through ChromelessButtonTemplatetoo. ChromelessButtonTemplateresource declaration can be found in Controls.Buttons.xamlfile.

我猜(还没有深入研究),图标是文本框的按钮部分。因此,您可能也需要通过ChromelessButtonTemplateChromelessButtonTemplate资源声明可以在Controls.Buttons.xaml文件中找到。

And about those 3 attached behaviors/properties (Watermark, ButtonContent, and ButtonCommand), definition can be found in TextboxHelper.csfile.

约3的那些附加的行为/特性(WatermarkButtonContent,和ButtonCommand),定义可参见TextboxHelper.cs文件。