将用户控件调整为堆栈面板的大小



我是WPF的新手,遇到了一个小问题。我有一个包含堆栈面板的用户控件。在堆栈面板内有多个richtxtbox,它将根据运行时的条件可见或折叠。问题是,用户控件保持相同的大小(我希望它在运行时是堆栈面板的大小(。我已经将高度和宽度属性设置为带有一些值的用户控件。我已经搜索了它,并得到了删除高度和宽度属性的建议。但这会使整个 wpf 面板崩溃。谁能帮忙,请?

设置常量大小不好 - 仅在需要时。无论如何,这是您的解决方案

<UserControl x:Class="WpfApp1.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" 
             xmlns:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Width="{Binding ElementName=MyStackPanel, Path=ActualWidth}"
             Height="{Binding ElementName=MyStackPanel, Path=ActualHeight}">
    <Grid>
        <StackPanel Width="350" 
                    Height="350" 
                    x:Name="MyStackPanel" 
                    Background="Blue" />
    </Grid>
</UserControl>

最新更新