Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Last active May 20, 2019 10:13
UserControl designed as modal presenter for the main window #wpf #csharp
<UserControl ...>
<UserControl.Resources>
<c:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
<c:NullToBoolConverter x:Key="NullToBoolConverter" />
<Style x:Key="ModalVisibility" TargetType="FrameworkElement">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ModalToShow, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Converter={StaticResource NullToBoolConverter}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding ModalToShow, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Converter={StaticResource NullToBoolConverter}}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<ContentControl>
<ContentPresenter Content="{Binding ViewToShow, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
</ContentControl>
<!-- The Rectangle is what simulates the modality -->
<Rectangle x:Name="Overlay" Opacity="0.4" Fill="LightGray" Style="{StaticResource ModalVisibility}" />
<Grid x:Name="Dialog" Style="{StaticResource ModalVisibility}">
<!-- The template for the dialog goes here (borders and such...) -->
<ContentPresenter x:Name="Modal" Content="{Binding ModalToShow, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</Grid>
</Grid>
</UserControl>
public partial class ModalPresenterControl : UserControl
{
public FrameworkElement ViewToShow
{
get { return (FrameworkElement)GetValue(ViewToShowProperty); }
set { SetValue(ViewToShowProperty, value); }
}
public static readonly DependencyProperty ViewToShowProperty =
DependencyProperty.Register(nameof(ViewToShow), typeof(FrameworkElement), typeof(ModalPresenterControl), new FrameworkPropertyMetadata(null));
public FrameworkElement ModalToShow
{
get { return (FrameworkElement)GetValue(ModalToShowProperty); }
set { SetValue(ModalToShowProperty, value); }
}
public static readonly DependencyProperty ModalToShowProperty =
DependencyProperty.Register(nameof(ModalToShow), typeof(FrameworkElement), typeof(ModalPresenterControl), new FrameworkPropertyMetadata(null));
public ModalPresenterControl()
{
InitializeComponent();
}
}
<ctrls:ModalPresenterControl ModalToShow="{Binding ActiveModal}">
<ctrls:ModalPresenterControl.ViewToShow>
<Grid>
<ContentPresenter Content="{Binding ActiveView, Mode=OneWay}"/>
</Grid>
</ctrls:ModalPresenterControl.ViewToShow>
</ctrls:ModalPresenterControl>
private DispatcherFrame frame;
private ContentControl activeView = new ContentControl();
public ContentControl ActiveView
{
get { return activeView; }
set { SetProperty(ref activeView, value); }
}
private FrameworkElement activeModal;
public FrameworkElement ActiveModal
{
get { return activeModal; }
set { SetProperty(ref activeModal, value); }
}
private void ShowModal()
{
frame = new DispatcherFrame();
ActiveModal = ...;
Dispatcher.PushFrame(frame);
}
private void HideModal()
{
frame.Continue = false;
frame = null;
ActiveModal = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment