WPF Custom Controls Are Invisible

♀尐吖头ヾ 提交于 2019-12-05 10:07:36

Where is your custom control template?

By saying

      DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton),
        new FrameworkPropertyMetadata(typeof(CustomButton)));

you're indicating you want to defined your own custom control. I think if you remove that, you'll see your button.

Christoph Löffelhardt

I suppose you have found a solution to your problem meanwhile. However, for the case, anyone else stumbles over the same problem as you did: The probably only explanation why a custom control does not show up although all the steps for creating it have been done correctly, as you did, is a missing entry in the AssemblyInfo.cs. This file must contain the following entry:

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly))]

Without this entry, the generic.xaml-file is ignored and therefore the default-control-template is not found, so the control will not get a control-template at all and therefore will not show up. This explains, too, why your control suddenly did show up when you disabled its static constructor. The line:

DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));

tells the control to use its own default-style instead of inheriting it from its base-class. So without this line the CustomButton will simply reuse the default control-template of the Button-class, with the consequence, that nothing you write into the generic.xaml will take any effect for the CustomButton.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!