我添加了两个按钮(一次仅可见一个)来调整 ContentDialog 的大小。我希望有两种尺寸:最大尺寸,其尺寸将调整为填充父窗口,或默认的“恢复”尺寸。它有点类似于标准 Windows 最大化和还原按钮。
我发现我可以在 XAML 中使用 ContentDialog.Resources 和“ContentDialogMaxWidth”和“ContentDialogMaxHeight”键来设置一种或其他所需的大小,如下所示。
XAML 中的默认大小:
<ContentDialog.Resources>
<x:Double x:Key="ContentDialogMaxWidth">800</x:Double>
<x:Double x:Key="ContentDialogMaxHeight">1000</x:Double>
</ContentDialog.Resources>
XAML 中的最大大小:
<ContentDialog.Resources>
<x:Double x:Key="ContentDialogMaxWidth">2000</x:Double>
<x:Double x:Key="ContentDialogMaxHeight">2000</x:Double>
</ContentDialog.Resources>
但是,上面的 XAML 仅在 ContentDialog 打开时设置一次大小。我似乎无法在打开后动态更改它。
下面是两个按钮的点击事件。我尝试在事件中动态更改上面显示的两个资源。即使资源值正在变化(我已经测试过),对话框大小也不会更新。
是否有另一种方法可以使用按钮事件将 ContentDialog 更改为所需的大小?
private void MaximizeDialogButton_Click(object sender, RoutedEventArgs e)
{
this.FullSizeDesired = true;
this.Resources["ContentDialogMaxWidth"] = 2000;
this.Resources["ContentDialogMaxHeight"] = 2000;
this.MaximizeDialogButton.Visibility = Visibility.Collapsed;
this.RestoreDialogButton.Visibility = Visibility.Visible;
}
private void RestoreDialogButton_Click(object sender, RoutedEventArgs e)
{
this.FullSizeDesired = false;
this.Resources["ContentDialogMaxWidth"] = 800;
this.Resources["ContentDialogMaxHeight"] = 1000;
this.Width = 800;
this.Height = 1000;
this.MaximizeDialogButton.Visibility = Visibility.Visible;
this.RestoreDialogButton.Visibility = Visibility.Collapsed;
}
似乎
ContentDialog
不支持这一点。相反,您可以使用 WinUIEx NuGet 包创建自定义对话框。
例如:
WindowDialog.xaml
<ex:WindowEx
x:Class="WinUIDemoApp.WindowDialog"
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:ex="using:WinUIEx"
xmlns:local="using:WinUIDemoApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid RowDefinitions="*,Auto">
<StackPanel
Grid.Row="0"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<NumberBox
x:Name="WidthNumberBox"
ValueChanged="WidthNumberBox_ValueChanged" />
<NumberBox
x:Name="HeightNumberBox"
ValueChanged="HeightNumberBox_ValueChanged" />
</StackPanel>
<Grid
Grid.Row="1"
ColumnDefinitions="*,*">
<Button
Grid.Column="0"
Click="OKButton_Click"
Content="OK" />
<Button
Grid.Column="1"
Click="CancelButton_Click"
Content="Cancel" />
</Grid>
</Grid>
</ex:WindowEx>
WindowDialog.xaml.cs
using Microsoft.UI.Xaml;
using System.Threading.Tasks;
using WinUIEx;
namespace WinUIDemoApp;
public enum WindowDialogResult
{
Cancel,
OK,
}
public partial class WindowDialog : WindowEx
{
private TaskCompletionSource<WindowDialogResult>? _taskCompletionSource;
public WindowDialog()
{
InitializeComponent();
IsAlwaysOnTop = true;
this.CenterOnScreen();
}
public new object? Content { get; set; }
public Task<WindowDialogResult> ShowAsync()
{
WidthNumberBox.Value = Width;
HeightNumberBox.Value = Height;
_taskCompletionSource = new();
this.Activate();
return _taskCompletionSource.Task;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
_taskCompletionSource?.SetResult(WindowDialogResult.OK);
this.Close();
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
_taskCompletionSource?.SetResult(WindowDialogResult.Cancel);
this.Close();
}
private void WidthNumberBox_ValueChanged(Microsoft.UI.Xaml.Controls.NumberBox sender, Microsoft.UI.Xaml.Controls.NumberBoxValueChangedEventArgs args)
{
Width = (float)sender.Value;
}
private void HeightNumberBox_ValueChanged(Microsoft.UI.Xaml.Controls.NumberBox sender, Microsoft.UI.Xaml.Controls.NumberBoxValueChangedEventArgs args)
{
Height = (float)sender.Value;
}
}
然后你可以像这样使用它:
WindowDialog dialog = new();
WindowDialogResult result = await dialog.ShowAsync();