Last active
May 20, 2019 10:13
UserControl designed as modal presenter for the main window #wpf #csharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ctrls:ModalPresenterControl ModalToShow="{Binding ActiveModal}"> | |
<ctrls:ModalPresenterControl.ViewToShow> | |
<Grid> | |
<ContentPresenter Content="{Binding ActiveView, Mode=OneWay}"/> | |
</Grid> | |
</ctrls:ModalPresenterControl.ViewToShow> | |
</ctrls:ModalPresenterControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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