要根据所需的客户区大小设置WPF窗口的大小,您可以使用以下方法:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="{Binding ClientWidth}"
Height="{Binding ClientHeight}">
<Grid>
<!-- 窗口内容 -->
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new WindowViewModel();
}
}
public class WindowViewModel : INotifyPropertyChanged
{
private double _clientWidth = 800;
public double ClientWidth
{
get { return _clientWidth; }
set
{
_clientWidth = value;
OnPropertyChanged();
}
}
private double _clientHeight = 600;
public double ClientHeight
{
get { return _clientHeight; }
set
{
_clientHeight = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class WindowViewModel : INotifyPropertyChanged
{
// ...
public void SetClientSize(double width, double height)
{
ClientWidth = width;
ClientHeight = height;
}
}
SetClientSize
方法来设置窗口大小:var windowViewModel = (WindowViewModel)DataContext;
windowViewModel.SetClientSize(1024, 768);
这样,您就可以根据所需的客户区大小设置WPF窗口的大小了。
领取专属 10元无门槛券
手把手带您无忧上云