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

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="260" Width="280" Background="Red"/>
    </Grid>
</UserControl>

使用:

<Grid Height="100" Width="100">
    <local:UserControl1/>
</Grid>



我想问的是:
在UserControl中,我设置的border1的尺寸Height是260,Width是280,在使用UserControl的时候,Grid的高和宽都只有100,为什么UserControl看起来没有被裁剪呢?

------解决方案--------------------
这还用问吗???你的用户控件的父容器Grid的宽和高都设置过了,子控件会根据这个范围进行收缩或者拉伸的。
------解决方案--------------------
如果想让剪切,使用Canva面板。
------解决方案--------------------
引用:
Quote: 引用:

这还用问吗???你的用户控件的父容器Grid的宽和高都设置过了,子控件会根据这个范围进行收缩或者拉伸的。

Grid,子控件会根据这个范围进行收缩或者拉伸的?那下面的代码,为什么Button没有拉伸(也设置了尺寸)

<Grid Height="100" Width="100">
    <Button Height="200" Width="200" Content="张三"/>
</Grid>



因为你这个button设置了宽和高,而你上边的UserControl没有设置宽和高。
------解决方案--------------------
你设置了ClipToBounds的值,它会进行裁剪~