WPF Window: 需要使用 SizeToContent 达到特定大小
WPF Window: Need to use SizeToContent up to a specific Size
我有一个 WPF window,其内容可以改变其大小(DataGrid)。
SizeToContent 适用于小型数据集,但对于大型数据集,window 在初始显示时变得太大。
我如何将 SizeToContent 与最大初始大小 600x600 结合使用(window 应该仍然可以手动调整为更大或最大化)。 window 也可以隐藏和重新显示多次,并且在重新显示时应该保持它的大小(这已经是我想保留的默认行为)。
myWindow.SizeToContent = SizeToContent.WidthAndHeight;
听起来你想要的是在第一次渲染时临时设置 MaxWidth 和 MaxHeight,但在第一次渲染后将其关闭。问题是,如果您在第一次渲染后关闭这些值,那么您的 SizeToContent 将启动并且 window 将再次调整为大型数据集!
为了缓解这种情况,您还必须在第一次渲染后关闭 SizeToContent。这是对我有用的解决方案:
MainWindow.xaml:
<Window x:Class="_72120715.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_72120715"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" SizeToContent="WidthAndHeight" MaxWidth="600" MaxHeight="600">
<Grid>
<ListBox ItemsSource="{Binding Stuff}"/>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public ObservableCollection<string> Stuff { get; set; }
public MainWindow()
{
InitializeComponent();
ContentRendered += MainWindow_ContentRendered;
DataContext = this;
Stuff = new ObservableCollection<string>();
for (int i = 0; i < 100; i++)
{
Stuff.Add($"This is stuff {i}");
}
}
private void MainWindow_ContentRendered(object sender, EventArgs e)
{
// Clear the MaxWidth/MaxHeight so that you can manually resize larger
MaxWidth = double.PositiveInfinity;
MaxHeight = double.PositiveInfinity;
// Clear the SizeToContent so that it doesn't automatically resize to large datasets
SizeToContent = SizeToContent.Manual;
}
}
我有一个 WPF window,其内容可以改变其大小(DataGrid)。 SizeToContent 适用于小型数据集,但对于大型数据集,window 在初始显示时变得太大。
我如何将 SizeToContent 与最大初始大小 600x600 结合使用(window 应该仍然可以手动调整为更大或最大化)。 window 也可以隐藏和重新显示多次,并且在重新显示时应该保持它的大小(这已经是我想保留的默认行为)。
myWindow.SizeToContent = SizeToContent.WidthAndHeight;
听起来你想要的是在第一次渲染时临时设置 MaxWidth 和 MaxHeight,但在第一次渲染后将其关闭。问题是,如果您在第一次渲染后关闭这些值,那么您的 SizeToContent 将启动并且 window 将再次调整为大型数据集!
为了缓解这种情况,您还必须在第一次渲染后关闭 SizeToContent。这是对我有用的解决方案:
MainWindow.xaml:
<Window x:Class="_72120715.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_72120715"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" SizeToContent="WidthAndHeight" MaxWidth="600" MaxHeight="600">
<Grid>
<ListBox ItemsSource="{Binding Stuff}"/>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public ObservableCollection<string> Stuff { get; set; }
public MainWindow()
{
InitializeComponent();
ContentRendered += MainWindow_ContentRendered;
DataContext = this;
Stuff = new ObservableCollection<string>();
for (int i = 0; i < 100; i++)
{
Stuff.Add($"This is stuff {i}");
}
}
private void MainWindow_ContentRendered(object sender, EventArgs e)
{
// Clear the MaxWidth/MaxHeight so that you can manually resize larger
MaxWidth = double.PositiveInfinity;
MaxHeight = double.PositiveInfinity;
// Clear the SizeToContent so that it doesn't automatically resize to large datasets
SizeToContent = SizeToContent.Manual;
}
}