wpf 绑定 material 设计对话框宽度,小于或超过 window 宽度

wpf binding material design dialog width, is either smaller or exceed window width

我想将对话框的宽度设置为覆盖整个父页面,在本例中是 UserControl,因此是 window。为此,我尝试将 material 设计对话框的宽度绑定到 UserControl 的 ActualWidth

只要 window 没有最大化,它就可以正常工作。

当它最大化时,对话框的宽度小于 UserControl 之一:

我尝试将 UserControl 的宽度绑定到 Window 的 ActualWidth (Width="{Binding ActualWidth, ElementName=win}"),但最糟糕的是对话框超过了 window : 我缺少什么?

TestWindow.xaml:

<Window x:Class="Wpf.Views.TestWindow" x:Name="win"
    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:Wpf.Views"
    mc:Ignorable="d"

    Title="TestWindow" Height="450" Width="800">
<Grid>
    <local:MyDialog/> <!-- Width="{Binding ActualWidth, ElementName=win}" -->
    </Grid>
</Window>

MyDialog.xaml

<UserControl x:Class="Wpf.Views.MyDialog" x:Name="self"
         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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         MinWidth="300"  MinHeight="300" mc:Ignorable="d">
<Grid>
    <materialDesign:DialogHost Identifier="1" BorderBrush="{DynamicResource MaterialDesignDivider}" Height="130" 
    Width="{Binding ActualWidth, ElementName=self}" HorizontalAlignment="Center">
        <Button Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" Width="130">
            Open
        </Button>
        <materialDesign:DialogHost.DialogContent>
            <StackPanel Orientation="Vertical" Height="130" Width="{Binding ActualWidth, ElementName=self}">
                    <TextBlock Text="Work in Progress" HorizontalAlignment="Center"/>
                    <ProgressBar Style="{DynamicResource MaterialDesignCircularProgressBar}"
      HorizontalAlignment="Center" Margin="16" IsIndeterminate="True" Value="0" />
                    <Button Style="{StaticResource MaterialDesignFlatButton}"
      IsCancel="True" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" HorizontalAlignment="Center">
                        CANCEL
                    </Button>
                </StackPanel>
        </materialDesign:DialogHost.DialogContent>
    </materialDesign:DialogHost>
    </Grid>
</UserControl>

我怀疑如果 window 除了被最大化之外还被放置在屏幕边缘旁边,您会看到类似的问题。原因是因为 DialogHost 控件包含一个 DialogMargin 属性,所有边默认为 35。出于您的目的,您可能希望将其设置为 0。