Reputation: 191
I'm trying to make a general custom control in WPF, and have other controls derive from it. The control is basically a square button that has a path for an icon, and that changes color when you hover it. The issue I have, is that I can neither get the base class nor the decendant class to render. No errors, no rendering.
The Generic.xaml contains this for the base class PlainButton
<Style TargetType="{x:Type local:PlainButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:PlainButton}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Border Background="{TemplateBinding BackgroundOverlay}" Padding="{TemplateBinding Padding}"/>
<Path HorizontalAlignment="Center" VerticalAlignment="Center" Stroke="{TemplateBinding Foreground}" StrokeThickness="2" Data="{TemplateBinding Icon}"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And this for the inheriting class CloseButton
<Style TargetType="{x:Type local:CloseButton}">
<Setter Property="Icon">
<Setter.Value>
<PathGeometry>
<PathFigure StartPoint="0,0">
<LineSegment Point="10,10" IsStroked="True"/>
<LineSegment Point="0,10" IsStroked="False"/>
<LineSegment Point="10,0" IsStroked="True"/>
</PathFigure>
</PathGeometry>
</Setter.Value>
</Setter>
</Style>
Icon and BackgroundOverlay are DPs in the PlainButton class. One control of each are placed in the main xaml as
<local:PlainButton BackgroundOverlay="Beige" Padding="10,10,10,10" Icon="M 0 0 L 0 10 L 10 10 z" Foreground="Black" Height="100" Canvas.Left="391" Canvas.Top="59" Width="100" Background="Blue" BorderBrush="Black" BorderThickness="10"/>
and
<local:CloseButton Padding="16,16,16,16" DockPanel.Dock="Right" Foreground="Black" MouseUp="Close"/>
Upvotes: 0
Views: 376
Reputation: 69959
Nobody can tell you what you problems are because you didn't show the code that defines either of your custom Button
s. However, the fact that you are attempting to create custom Button
s at all leads me to believe that you are new to WPF. I say this because in WPF, there is little need to extend UI controls, because this wonderful language provides so many other ways for us to customise a built in control without having to extend it.
I would therefore recommend that you take some time to have a good read of the Control Authoring Overview page on MSDN, instead of continuing along your current path. Here is an introductory excerpt from the linked page:
Historically, if you wanted to get a customized experience from an existing control, you were limited to changing the standard properties of the control, such as background color, border width, and font size. If you wished to extend the appearance or behavior of a control beyond these predefined parameters, you would need to create a new control, usually by inheriting from an existing control and overriding the method responsible for drawing the control. Although that is still an option, WPF enables to you customize existing controls by using its rich content model, styles, templates, and triggers. The following list gives examples of how these features can be used to create custom and consistent experiences without having to create a new control.
Upvotes: 2