Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Custom Controls Are Invisible

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:

  1. Create a new WPF Application
  2. Add -> New Item... -> Custom Control (WPF): "CustomButton.cs"
  3. I change the CustomButton base class to Button instead of Control
  4. Add a CustomButton control to my main window.
  5. When I run the application or view the main window in the designer, I don't see anything.

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).

like image 411
Jason Avatar asked Jul 14 '11 15:07

Jason


2 Answers

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.

like image 140
Christoph Löffelhardt Avatar answered Nov 20 '22 05:11

Christoph Löffelhardt


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.

like image 12
Jeff Avatar answered Nov 20 '22 06:11

Jeff