簡體   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