Skip to content

Commit

Permalink
added support to control DialogWindow from style
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed Feb 28, 2019
1 parent 04cc589 commit 6494cf4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sandbox/Wpf/HelloWorld/HelloWorld/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterDialog<ConfirmationDialog, ConfirmationDialogViewModel>();

//register a custom window host
containerRegistry.RegisterDialogWindow<CustomDialogWindow>();
//containerRegistry.RegisterDialogWindow<CustomDialogWindow>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Width="300" Height="150">

<prism:DialogService.DialogWindowStyle>
<Style TargetType="Window">
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="ShowInTaskbar" Value="False"/>
<Setter Property="SizeToContent" Value="WidthAndHeight"/>
</Style>
</prism:DialogService.DialogWindowStyle>

<Grid x:Name="LayoutRoot" Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
Expand Down
1 change: 1 addition & 0 deletions Source/Wpf/Prism.Wpf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Mvvm")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Interactivity")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Interactivity.InteractionRequest")]
[assembly: XmlnsDefinition("http://prismlibrary.com/", "Prism.Services.Dialogs")]


Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Title}" Icon="{Binding IconSource}"
WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight">

WindowStartupLocation="CenterOwner">
<Window.Style>
<Style TargetType="{x:Type Window}" >
<Setter Property="SizeToContent" Value="WidthAndHeight" />
</Style>
</Window.Style>

</Window>
35 changes: 30 additions & 5 deletions Source/Wpf/Prism.Wpf/Services/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ public class DialogService : IDialogService
{
private readonly IContainerExtension _containerExtension;

public static readonly DependencyProperty DialogWindowStyleProperty =
DependencyProperty.RegisterAttached("DialogWindowStyle", typeof(Style), typeof(DialogService), new PropertyMetadata(null));

public static Style GetDialogWindowStyle(DependencyObject obj)
{
return (Style)obj.GetValue(DialogWindowStyleProperty);
}

public static void SetDialogWindowStyle(DependencyObject obj, Style value)
{
obj.SetValue(DialogWindowStyleProperty, value);
}

public DialogService(IContainerExtension containerExtension)
{
_containerExtension = containerExtension;
Expand All @@ -31,8 +44,6 @@ void ShowDialogInternal(string name, IDialogParameters parameters, Action<IDialo
IDialogWindow dialogWindow = CreateDialogWindow();
ConfigureDialogWindowEvents(dialogWindow, callback);
ConfigureDialogWindowContent(name, dialogWindow, parameters);

dialogWindow.Owner = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);

if (isModal)
dialogWindow.ShowDialog();
Expand All @@ -56,10 +67,9 @@ void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDial
if (viewModel == null)
throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");

MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
ConfigureDialogWindowProperties(window, dialogContent, viewModel);

window.Content = dialogContent;
window.DataContext = viewModel; //we want the host window and the dialog to share the same data contex
MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
}

void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, Action<IDialogResult> callback)
Expand Down Expand Up @@ -106,5 +116,20 @@ void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, Action<IDialogResul
};
dialogWindow.Closed += closedHandler;
}

void ConfigureDialogWindowProperties(IDialogWindow window, FrameworkElement dialogContent, IDialogAware viewModel)
{
var windowStyle = DialogService.GetDialogWindowStyle(dialogContent);
if (windowStyle != null)
window.Style = windowStyle;

window.Content = dialogContent;
window.DataContext = viewModel; //we want the host window and the dialog to share the same data contex

//TODO: is there a better way to set the owner
window.Owner = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);

//TODO: is the a good way to control the WindowStartupPosition (not a dependency property and can't be set in a style)
}
}
}
2 changes: 2 additions & 0 deletions Source/Wpf/Prism.Wpf/Services/Dialogs/IDialogWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public interface IDialogWindow
event CancelEventHandler Closing;

IDialogResult Result { get; set; }

Style Style { get; set; }
}
}

0 comments on commit 6494cf4

Please sign in to comment.