c# - wpf绑定材料设计对话框宽度,小于或超过窗口宽度
问题描述
我想设置对话框的宽度以覆盖整个父页面,在这种情况下是用户控件,因此是窗口。为此,我尝试将材料设计对话框的宽度绑定到ActualWidth
UserControl 的宽度。
只要窗口未最大化,它就可以正常工作。
当它最大化时,对话框的宽度小于 UserControl 之一:
我试图将 UserControl 的宽度绑定到ActualWidth
Window ( 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>
解决方案
我怀疑如果窗口除了被最大化之外还被放置在屏幕边缘旁边,你会看到类似的问题。原因是因为 DialogHost 控件包含一个DialogMargin属性,该属性在所有方面都默认为 35。出于您的目的,您可能希望将其设置为 0。
推荐阅读
- javascript - 未找到 React 模块 - 如何在反应中导入模块
- html - 在 Django 视图和模板中执行一些算术后,我得到了错误的值
- python - 为什么在 __exit__ 函数中更改引用不起作用?
- laravel - Laravel 返回注册 ID
- ios - 不要在选项卡开关的导航中重新加载顶部视图中的内容
- c - C API 允许自动和分配的存储
- loops - Python:While循环索引超出范围
- node.js - multer保存文件后修改req.body
- javascript - 使用给定模式验证日期输入 - JS / React
- php - 向 array_map 中的 html_entity_decode 添加标志