元素wpf的初始大小

元素wpf的初始大小,wpf,Wpf,我有一个带有此标记的窗口 <Window x:Class="TestCloseWindow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <Button C

我有一个带有此标记的窗口

<Window x:Class="TestCloseWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">

        <Button Command="ApplicationCommands.Close">Foo</Button>

</Window>
我想要最小宽度和最小高度100的纽扣 最大高度和最小高度等于500

并且喜欢大小窗口的内容,所以我做这个

<Window x:Class="TestCloseWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight" >

        <Button MaxHeight="500" MaxWidth="500" MinHeight="100" MinWidth="100" Command="ApplicationCommands.Close">Foo</Button>

</Window>
我想设置按钮200 x 200的初始尺寸 没有在窗口中写入宽度和高度

如果我在按钮中写入Width=“200”Height=“200”,则按钮将变得不可编辑


如何设置控件的初始大小?

我不确定您到底想实现什么,但看看这是否符合您的要求。我使用一个视图框来拉伸窗口大小时的内容,并设置窗口中的约束。请注意,窗口大小不同于窗口客户端区域大小

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  MaxHeight="500" MaxWidth="500" 
        MinWidth="100" MinHeight="100" 
        Width="200" Height="200">
    <Viewbox StretchDirection="Both" Stretch="Fill">
        <Button >Hello</Button>
    </Viewbox>

你好

所以你想让元素知道你想要它的大小而不告诉它吗?@sa_ddam213我想知道元素的初始大小。如果我放置Width=“200”Height=“200”,元素将变得不可压缩