2

当您在 WPF 设计器中拖放控件时,Visual Studio 2010 会创建默认属性值。

<UserControl x:Class="TestPanel.UserControl1"
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>

Height=23 和 Width=75 由设计者自动生成。

有谁知道设计师从哪里获得这些值,是否可以在自定义应用程序中运行它们?

4

1 回答 1

0

两者都是,Height并且当您为控件注册依赖属性时,您可以设置or可以使用的依赖属性的默认值。WidthDependency PropertiesVisual StudioXAML designer

默认值在FrameworkPropertyMatadata类构造函数中设置,如下代码所示:

//Default value is set to Top
Control.VerticalContentAlignmentProperty = DependencyProperty.Register("VerticalContentAlignment", typeof(VerticalAlignment), typeof(Control), new FrameworkPropertyMetadata(VerticalAlignment.Top), new ValidateValueCallback(FrameworkElement.ValidateVerticalAlignmentValue));

//Default value is set to Left
Control.HorizontalContentAlignmentProperty = DependencyProperty.Register("HorizontalContentAlignment", typeof(HorizontalAlignment), typeof(Control), new FrameworkPropertyMetadata(HorizontalAlignment.Left), new ValidateValueCallback(FrameworkElement.ValidateHorizontalAlignmentValue));
于 2013-09-06T15:31:11.383 回答