繁体   English   中英

使用MahApps保持样式在WPF中扩展TextBox

[英]Extending TextBox in WPF using MahApps keeping Style

我制作了一个自定义文本框类,用于验证用户的输入,使其仅允许使用十六进制值,并在xaml中使用了此新文本框(HexTextBox)。 它运作良好,但HexTextBox放弃了Mahapps的所有样式,包括配色方案和TextBoxHelper。 您知道如何使用扩展的TexBox并保持样式吗?

HexTextBox:

    public class HexTextBox : TextBox
    {
    public HexTextBox()
    {

    }
    /// <summary>
    /// Raise when a keyboard key is pressed.
    /// </summary>
    /// <param name="e">The event args.</param>
    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Space)
        {
            e.Handled = true;
        }

        base.OnPreviewKeyDown(e);
    }

    /// <summary>
    /// Raise when a text will be inputed in the text box object.
    /// </summary>
    /// <param name="e">The event args.</param>
    protected override void OnTextInput(TextCompositionEventArgs e)
    {
        int hexNumber;

        e.Handled = !int.TryParse(e.Text, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out hexNumber);

        base.OnTextInput(e);
    }
}

Window.xaml

<UserControl
...
    xmlns:CoreWPF="clr-namespace:CoreWPF;assembly=CoreWPF" 
...>

<CoreWPF:HexTextBox 
        Text="{Binding DataXor1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        Grid.Column="2" Grid.Row="0"
        controls:TextBoxHelper.ClearTextButton="True"
        Height="26"
        TextWrapping="Wrap" 
        CharacterCasing="Upper"
        VerticalAlignment="Center"/>

提前致谢!

为自定义控件创建默认样式,该样式将基于TextBox样式。

<Style TargetType="Controls:HexTextBox" BasedOn="{StaticResource {x:Type TextBox}}"/>

暂无
暂无

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

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