c# - 如何在一个 Grid 上创建 DialogHost 并在另一个 Grid 上调用
问题描述
我正在寻找一种方法让我DialogHost
在 WPF 应用程序的中间居中,因为我需要在MainGrid
.
<Grid Name="MainGrid">
<materialDesign:DialogHost CloseOnClickAway="True">
<materialDesign:DialogHost.DialogContent>
<Grid Margin="20">
<TextBlock Text="My first Dialog" />
</Grid>
</materialDesign:DialogHost.DialogContent>
</materialDesign:DialogHost>
<Grid Name="Grid1" Width="250" Height ="Auto">
... //MyCode
<Button Name="MyButton" Style="{StaticResource MaterialDesignRaisedButton}" Click="ButtonClick">
...
</Grid>
</Grid>
但是,我将用来调用它的按钮DialogHost
位于Grid1
.
有可能这样做吗?如果是这样,怎么做?
解决方案
您可以使用DialogHost.OpenDialogCommand
Material Design 提供的 routed 命令。如果您将其分配Command
给您的按钮,它将引发一个路由事件,该事件会在可视树中冒泡,直到它到达DialogHost
通过显示对话框来处理它的事件。例如:
<Button Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}">
由于此机制通过搜索父对话主机来工作,因此您必须将视图分配为它的内容。这意味着将您的Grid
内部DialogHost
标签移到DialogContent
.
对于您的情况,您可以将DialogHost
直接放入窗口或您的MainGrid
并将您的Grid1
放入对话框主机内容中。最后,您只需将 分配OpenDialogCommand
给按钮。
<Window ...>
<Grid Name="MainGrid">
<materialDesign:DialogHost CloseOnClickAway="True">
<materialDesign:DialogHost.DialogContent>
<Grid Margin="20">
<TextBlock Text="My first Dialog" />
</Grid>
</materialDesign:DialogHost.DialogContent>
<Grid Name="Grid1" Width="250" Height ="Auto">
<Button Name="MyButton"
Style="{StaticResource MaterialDesignRaisedButton}"
Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}">
</Grid>
</materialDesign:DialogHost>
</Grid>
</Window>
推荐阅读
- jquery - 如何按日期对我的 jquery 数据表进行排序?
- javascript - 如何避免使用 loginPopup 或 loginRedirect 并静默获取经过身份验证的
- ruby-on-rails - 运行测试时出现不推荐使用的错误,即使测试结果很好
- python - 单元测试使用自变量的函数
- javascript - 如何在 Firebase 中调试身份验证?
- postgresql - 分区删除时的 Postgres 分区查询性能
- python - setup.py 安装操作系统依赖
- python - 从 Python2.7 代码到 Python3 的 Python API 转换
- php - Symfony $form->getData() 返回值连接的空对象
- angular - Angular:测试用例 - jasmine - TypeError:无法读取未定义的属性“cmd”