2015-08-26 77 views
2

擴展文本框在WPF我做了一個自定義文本框類用於驗證用戶的輸入,以僅允許十六進制值,並且在使用XAML這個新的文本框(HexTextBox)。它運作良好,但HexTextBox失去一切從Mahapps的風格,包括配色方案和TextBoxHelper。你知道如何使用這個擴展的TexBox並保持風格嗎?使用MahApps保存風格

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"/> 

提前感謝!

回答

4

爲此將基於文本框樣式定製控件創建默認樣式。

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

它的工作!非常感謝你! –

+0

@Lucas真高興我幫了忙。請不要忘記標記答案。 –