2016-12-14 3 views
2

Ich arbeite an einem modalen Popup-Dialogfeld (Ich bin nicht sicher über die genaue UX Begriff), die inline angezeigt wird, in einem Steuerelement oder Fenster mit dunkler Hintergrund .Einfache Popup-Dialog in WPF (Overlay im Fenster)

Visuelles Beispiel

example

Was ich versuchte ein <ContentPresenter /> in der XAML des Popup setzen und dann instanziiert es wie folgt aus:

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

jedoch die XAML ersetzt die gesamte Popup XAML statt platziert zu werden, wo der Content ist.

Q: Wie wird der ContentPresenter hier richtig verwendet?

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

Antwort

2

Der Inhalt Ihrer Popup sollte als Control für den Content definiert werden, wie hier erwartet zu arbeiten. Bitte beachten Sie den folgenden Beispielcode.

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

Die '' tat die ganze Trick. Vielen Dank! +1 – bytecode77