如何在VS2010 WPF设计器中控制自定义控件的默认属性

发布于 2024-10-04 08:12:16 字数 1010 浏览 0 评论 0原文

我有一个继承自 Button 的类。在该类的 XAML 中,我指定了宽度、高度和内容,希望当我使用 VS2010 WPF 设计器将控件插入到窗口等中时,这些将成为这些属性的默认值。但是,设计者使用 Button 的默认值。

我的控件 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>

设计器生成的 XAML:

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

我需要做什么来控制设计器默认值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯离梦残月幽梦 2024-10-11 08:12:16

这就是 DependencyProperty.OverrideMetadata 的用途。

在静态构造函数中,您将调用 OverrideMetadata,并传递带有新默认值的 FrameworkPropertyMetadata。例如,这将 Button 的默认宽度设置为 60

public class NewButton : Button
{
   static NewButton()
   {
        WidthProperty.OverrideMetadata(typeof(NewButton), new FrameworkPropertyMetadata((double)60));
   }
}

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));
   }
}
凉薄对峙 2024-10-11 08:12:16

删除 Height="64" Width="64"DesignHeightDesignWidth 就足够了。

编辑:
在您的控件构造函数中。通过检查控件是否处于设计模式来初始化所需的默认值。

public FunctionButton4()
{
   if(DesignerProperties.GetIsInDesignMode)
   {
       this.MinHeight = 30d; /// Min Height or Height whichever is your concern
       this.MinWidth = 75d;
   }
}

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;
   }
}
抠脚大汉 2024-10-11 08:12:16

我以前没有使用过它,但我相信这就是 DependencyProperty.OverrideMetadata 是为了。

在您的类中,您将添加一个静态构造函数,并在该静态构造函数中,您将调用 OverrideMetadata,传递带有新默认值的 PropertyMetadata。例如:(理论,未经测试)

static FunctionButton4() {
    WidthProperty.OverrideMetadata(
        typeof(FunctionButton4), new PropertyMetadata(64));
    HeightProperty.OverrideMetadata(
        typeof(FunctionButton4), new PropertyMetadata(64));
}

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文