简体   繁体   中英

WPF styling custom control

I have a class that inherits from TextBox

public class DecimalTextBox : TextBox
{

    #region Float Color 
    public static readonly DependencyProperty FloatColorProperty = DependencyProperty.Register("FloatColor", typeof(Color), typeof(DecimalTextBox), new FrameworkPropertyMetadata(Colors.Red));
    public Color FloatColor
    {
        get { return (Color)GetValue(FloatColorProperty); }
        set { SetValue(FloatColorProperty, value); }
    }
    #endregion

    . . . . ..  OTHER STUFF
}

I want to style this control using something like this:

<Style x:Key="DecimalTextBoxGridStyle" TargetType="DecimalTextBox">
    <Setter Property="TextAlignment" Value="Right"/>
    <Setter Property="FloatColor" Value="Black"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
</Style>

But it style told me

在此处输入图片说明

DecimalTexbox type isn't admited in wpf project

How can I do to that ?

There is another approach ?

Include the XAML namespace:

<Style x:Key="DecimalTextBoxGridStyle" TargetType="local:DecimalTextBox">

where local is mapped to the CLR namespace in which the DecimalTextBox class is defined:

<Window ...
    xmlns:local="clr-namespace:WpfApplication1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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