wpf - 紧密围绕用户控制的窗口

And*_*rke 1 wpf wpf-controls

我有一个用户控件,我正在动态加载到一个窗口 - 我想设置窗口,使它没有大小,然后我认为窗口相应调整大小取决于UserControl.但它不是 - 任何人都可以协助吗?

我已经做了一个非常基本的例子 - 我已经删除了动态位,只是将一个UserControl放在一个Window中.为了让UserControl周围的窗口紧张,我需要做些什么?

谢谢,

安迪

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="LightBlue">
    <Grid>
    </Grid>
</UserControl>


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="Window1"  >
    <Grid>
        <WpfApplication1:UserControl1>
        </WpfApplication1:UserControl1>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

apa*_*dit 6

尝试在窗口上将SizeToContent设置为WidthAndHeight.

请参阅MSDN页面