c# - WPF 中的简单弹出对话框(窗口内覆盖)

标签 c# .net wpf xaml contentpresenter

我正在开发一个模态对话框弹出窗口(我不确定确切的 UX 术语),它以内联方式显示在背景较暗的控件或窗口内。

视觉示例

example

我尝试的是放一个 <ContentPresenter />在弹出窗口的 XAML 中,然后像这样实例化它:

<local:Popup Grid.RowSpan="2">
    <TextBlock Text="Popup test..." />
</local:Popup>

但是,XAML 替换了整个 Popup XAML,而不是放置在 ContentPresenter 所在的位置。

问:这里的ContentPresenter如何正确使用?

Popup.xaml

<ContentControl
    x:Class="[...].Popup"
    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:local="clr-namespace:[...]"
    mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="300">
    <Grid Background="#7f000000">
        <Grid Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
            <StackPanel Margin="20">
                <TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType=UserControl}}" FontSize="20" />
                <ContentPresenter />
            </StackPanel>
        </Grid>
    </Grid>
</ContentControl>

Popup.xaml.cs

using System.Windows;

namespace [...]
{
    public partial class Popup : ContentControlBase
    {
        public static DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(Popup));
        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public Popup()
        {
            InitializeComponent();
        }
    }
}

最佳答案

您的 Popup 的内容应该被定义为一个 ControlTemplate,以便 ContentPresenter 在这里按预期工作。请引用以下示例代码。

弹出.xaml:

<ContentControl
x:Class="WpfApplication1.Popup"
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:local="clr-namespace:WpfApplication1"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="300"
x:Name="popup">
<ContentControl.Template>
    <ControlTemplate TargetType="local:Popup">
        <Grid Background="#7f000000">
            <Grid Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
                <StackPanel Margin="20">
                    <TextBlock Text="{Binding Title, ElementName=popup}" FontSize="20" />
                    <ContentPresenter />
                </StackPanel>
            </Grid>
        </Grid>
    </ControlTemplate>
</ContentControl.Template>

Popup1.xaml.cs.

public partial class Popup : ContentControl
{
    public static DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(Popup));
    public string Title
    {
        get
        {
            return (string)GetValue(TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, value);
        }
    }

    public Popup()
    {
        InitializeComponent();
    }
}

Window1.xaml:

<local:Popup Title="Title...">
     <TextBlock>Text...</TextBlock>
</local:Popup>

关于c# - WPF 中的简单弹出对话框(窗口内覆盖),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41150483/

相关文章:

.net - .NET 对象创建是 "ex nihilo"吗?

c# - 将 SecureString 放入 PasswordBox

wpf - 如何将 DataTemplateSelector 与 DataGridBoundColumn 一起使用?

c# - 获取对象的完整字符串表示形式(如在 Visual Studio 的即时窗口中)

.net - 正则表达式文件扩展名过滤器

c# - Path.Combine 背后究竟发生了什么

c# - 生成随机的唯一值 C#

c# - 从指定列和行中的数据网格收集数据

c# - Visual Studio 2017 社区版上的类图在哪里

c# - IBotToUser 实现停止事件日志记录