When I create a custom control in WPF and add it to a window, I don't see anything where I placed it in the dialog. Here's what I'm doing:
Here's what the code looks like.
CustomButton.cs:
public class CustomButton : Button
{
static CustomButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton),
new FrameworkPropertyMetadata(typeof(CustomButton)));
}
}
MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
<Grid>
<my:CustomButton Content="Hello World" x:Name="customButton1"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="150,175,0,0" />
</Grid>
</Window>
Generic.xaml:
<Style TargetType="{x:Type local:CustomButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomButton}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've found two leads as to what's going on, but nothing has clicked yet. When I added the custom control, Visual Studio added Themes/Generic.xaml, but no matter what I try in there, I see no difference on screen. The other thing is that if I comment out the static constructor in CustomButton.cs, all of a sudden the button show up in the main window. It doesn't look quite right in all situations, though (like if I use the button in a toolbar).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With