2016-12-14 183 views
2

我正在模态对话框中弹出(我不确定关于确切的UX术语)以内联方式显示,在带有背景变暗的控件或窗口内。WPF中的简单弹出对话框(覆盖在窗口内)

视觉例如

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(); 
     } 
    } 
} 

回答

2

您的弹出窗口的内容应该定义为一个控件模板的ContentPresenter来这里正常工作。请参考下面的示例代码。

Popup.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> 
+0

的''做了所有的特技。谢谢! +1 – bytecode77