2

I have a class that inherits from Button. In the XAML for the class, I have specified a Width and Height and Content, in the hope that when I use the VS2010 WPF designer to insert my control in e.g. a window, these would be the default values for those properties. However, the designer uses the default values from Button.

My control XAML:

<Button x:Class="Something.FunctionButton4"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="64" d:DesignWidth="64"
    Height="64" Width="64" Content="FunctionButton"
    OverridesDefaultStyle="True"
    Focusable="False">
  <Button.Template>
    <ControlTemplate TargetType="Button">
      ...
    </ControlTemplate>
  </Button.Template>
</Button>

Designer generated XAML:

<my:FunctionButton4 Content="Button" Height="23" x:Name="functionButton43" Width="75" />

What do I need to do to control the designer defaults?

NPVN
  • 95
  • 2
  • 8
  • Possible duplicate of http://stackoverflow.com/questions/75495/wpf-usercontrol-design-time-size – Craig Suchanec Apr 26 '11 at 01:35
  • @Craig, the question you linked to seems to be about the opposite: it's about *not* using the control's width and height (at runtime), whereas this question is about *wanting* to use them (at design time). – Joe White Apr 26 '11 at 03:18
  • @Joe, whoops so it is. I misread that. Same principals should apply, but it might still be helpful. – Craig Suchanec Apr 26 '11 at 03:34

3 Answers3

1

This is what DependencyProperty.OverrideMetadata is for.

In the static constructor, you would call OverrideMetadata, passing a FrameworkPropertyMetadata with your new default. For example, this sets the default width for a Button to be 60

public class NewButton : Button
{
   static NewButton()
   {
        WidthProperty.OverrideMetadata(typeof(NewButton), new FrameworkPropertyMetadata((double)60));
   }
}
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
0

Remove Height="64" Width="64". The DesignHeight and DesignWidth would be enough.

Edit: In your controls constructor. Initialize the desired defaults, by checking whether the control is in design mode like.

public FunctionButton4()
{
   if(DesignerProperties.GetIsInDesignMode)
   {
       this.MinHeight = 30d; /// Min Height or Height whichever is your concern
       this.MinWidth = 75d;
   }
}
Prince Ashitaka
  • 8,623
  • 12
  • 48
  • 71
  • No, they also do nothing. They only have an effect on how the control itself appears in the designer. – NPVN Nov 26 '10 at 09:29
  • Could you please elaborate more. – Prince Ashitaka Nov 26 '10 at 10:19
  • The problem occurs when I want to use my control in e.g. a window: In the Designer, I open the Toolbox, select my control, and then click in the window. The control that gets inserted has Width=75 in stead of Width=64, and likewise Height and Content are not what I want as default. The defaults used (e.g. Width=75) are the defaults for Button, which my control is derived from. – NPVN Nov 26 '10 at 12:18
  • I have updated code snippet. Check whether is that what you need. – Prince Ashitaka Nov 26 '10 at 13:05
  • Sadly not. If I use 64 for MinHeight and MinWidth, I get the peculiar behavior that the control is shown as 75x64, but in the Properties pane, Height is shown as 25!? The 75x64 seems reasonable, since I am now preventing it from setting a value below 64, but the inconsistency with the Properties pane is a bit disturbing... – NPVN Nov 29 '10 at 07:55
  • If you want to restrict the Height to certain amount. Then, you should use property coercion to set the value. – Prince Ashitaka Nov 29 '10 at 09:54
  • I think you misunderstand my problem a bit, let me try to rephrase it: – NPVN Dec 01 '10 at 09:26
  • I think you misunderstand my problem a bit, let me try to rephrase it: I don't want to restrict the size as such, I'd just like to set a reasonable default that the Designer will use when I place my control in a window. For example, when I place a new Button in a window, it is assigned a Width of 75 and a Height of 23. If I place a GroupBox, it is assigned a Width of 200 and a Height of 100. Clearly, it is possible for a a control to specify a default size. This is not a restriction on the size, because I can change the size of my Button later. BTW thanks for keeping on trying to help! – NPVN Dec 01 '10 at 09:33
  • Well, yes, it is in the post!? The designer inserts a Width and Height specification when you add a control into a window, and I would like to tell the designer what the default for those should be. The Button that I derive from apparently does this, to get 75 and 23, respectively. – NPVN Dec 01 '10 at 13:09
0

I haven't used it before, but I believe this is what DependencyProperty.OverrideMetadata is for.

In your class, you would add a static constructor, and in that static constructor, you would call OverrideMetadata, passing a PropertyMetadata with your new default. For example: (theoretical, untested)

static FunctionButton4() {
    WidthProperty.OverrideMetadata(
        typeof(FunctionButton4), new PropertyMetadata(64));
    HeightProperty.OverrideMetadata(
        typeof(FunctionButton4), new PropertyMetadata(64));
}
Joe White
  • 94,807
  • 60
  • 220
  • 330