如何在VS2010 WPF设计器中控制自定义控件的默认属性
我有一个继承自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是 DependencyProperty.OverrideMetadata 的用途。
在静态构造函数中,您将调用 OverrideMetadata,并传递带有新默认值的
FrameworkPropertyMetadata
。例如,这将 Button 的默认宽度设置为 60This 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删除
Height="64" Width="64"
。DesignHeight
和DesignWidth
就足够了。编辑:
在您的控件构造函数中。通过检查控件是否处于设计模式来初始化所需的默认值。
Remove
Height="64" Width="64"
. TheDesignHeight
andDesignWidth
would be enough.Edit:
In your controls constructor. Initialize the desired defaults, by checking whether the control is in design mode like.
我以前没有使用过它,但我相信这就是 DependencyProperty.OverrideMetadata 是为了。
在您的类中,您将添加一个静态构造函数,并在该静态构造函数中,您将调用 OverrideMetadata,传递带有新默认值的 PropertyMetadata。例如:(理论,未经测试)
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)