【问题标题】:How to create DialogHost on one Grid and call on another Grid如何在一个 Grid 上创建 DialogHost 并在另一个 Grid 上调用
【发布时间】:2021-02-24 18:50:18
【问题描述】:

我正在寻找一种方法使我的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

有可能这样做吗?如果有,怎么做?

【问题讨论】:

    标签: c# .net wpf xaml material-design-in-xaml


    【解决方案1】:

    您可以使用 Material Design 提供的DialogHost.OpenDialogCommand routed 命令。如果你将它作为Command 分配给你的按钮,它将引发一个路由事件,该事件会在可视树中冒泡,直到它到达DialogHost,通过显示对话框来处理它。例如:

    <Button Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}">
    

    由于此机制通过搜索 对话主机起作用,因此您必须为其分配views as content。这意味着将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>
    

    【讨论】:

    • Grid1 是一个列,其中我有几个按钮、图像、文本、字段、复选框……这些 Grid1 代码在 materialDesign:DialogHost 内会有问题吗?
    • @Michael 不,对话主机只是充当容器,应该没有任何问题。它是这样设计的,以便您可以从内容中执行打开命令,这将引发一个路由事件,该事件只有在 DialogHost 是相应按钮的父级时才能处理。
    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 2020-08-20
    • 2018-03-06
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多