日期:2014-05-17  浏览次数:21239 次

WPF,请问UserControl的尺寸和布局
一个UserControl:

<UserControl x:Class="WPF1.UserControl1"
             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="300" d:DesignWidth="300" Background="Blue">
    <Grid>
        <Border Name="border1" Margin="10" Height="200" Width="100" Background="Red"/>
    </Grid>
</UserControl>

设计视图:

使用UserControl:

<Window x:Class="WPF1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF1"
        Title="MainWindow" Height="350" Width="525">
    <Grid Height="50" Width="200">
        <local:UserControl1 />
    </Grid>
</Window>



我想问的是:
在UserControl的模板中,border1的Height的尺寸是Width的2倍,但是在使用UserControl的时候,模板中的Border看起来其Height却比Width还小呢?

------解决方案--------------------
默认把你的东西给截取了
在你使用的地方,这个控件的容器只有这么大<Grid Height="50" Width="200">

其实是高度被截取了,宽度没有被截取
------解决方案--------------------
容器grid设置了宽和高所致
------解决方案--------------------
你的用户控件看着大 是因为有两个专属于设计视图的属性
d:DesignHeight="300" d:DesignWidth="300" 
你自己看一看
为什么引用后就变小了是因为
<Grid Height="50" Width="200">
        <local:UserControl1 />
    </Grid>
你设定了他的容器控件的宽和高了 所以他必须是height = 50 width = 200的样子
------解决方案----