简体   繁体   中英

wpf create a style for a custom control

I have a custom control that inherits from another (third party) control:

public class Int32EditBox : Xceed.Wpf.Toolkit.IntegerUpDown { }

Then, I tried to create a style for this control:

xmlns:Controls="clr-namespace:MyApp.Controls;assembly=MyApp.Controls"
...     
<Style TargetType="{x:Type Controls:Int32EditBox}" BasedOn="{StaticResource {x:Type Controls:Int32EditBox}}">
            <Setter Property="ContextMenu" Value="{StaticResource DefaultDesignerContextMenu}" />
            <Setter Property="IsReadOnly" Value="True" />
          </Style>

The problem is that I get an error that says the resource "MyApp.Control.Int32EditBox" cannot be found.

Does anybody know why I'm getting this error?

Add this to the static constructor of your custom control, otherwise it will use the style for Xceed.Wpf.Toolkit.IntegerUpDown or whatever base class has set the default style key:

static Int32EditBox()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(Int32EditBox), new FrameworkPropertyMetadata(typeof(Int32EditBox)));
}

And in order for the BasedOn to work, there must be another style with that key within the visual tree.

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