首页 > 解决方案 > wpf绑定材料设计对话框宽度,小于或超过窗口宽度

问题描述

我想设置对话框的宽度以覆盖整个父页面,在这种情况下是用户控件,因此是窗口。为此,我尝试将材料设计对话框的宽度绑定到ActualWidthUserControl 的宽度。

只要窗口未最大化,它就可以正常工作。

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

我试图将 UserControl 的宽度绑定到ActualWidthWindow ( Width="{Binding ActualWidth, ElementName=win}") 的宽度,但最糟糕的是对话框超出了窗口:我错过了什么?

测试窗口.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>

屏幕边缘

标签: c#wpfxamlwpf-controlsmaterial-design-in-xaml

解决方案


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


推荐阅读