Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support to control DialogWindow propertes from style #1721

Merged
merged 2 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions Source/Wpf/Prism.Wpf/Ioc/IContainerRegistryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ namespace Prism.Ioc
{
public static class IContainerRegistryExtensions
{
/// <summary>
/// Registers an object to be used as a dialog in the IDialogService.
/// </summary>
/// <typeparam name="TView">The Type of object to register as the dialog</typeparam>
/// <param name="containerRegistry"></param>
/// <param name="name">The unique name to register with the dialog.</param>
public static void RegisterDialog<TView>(this IContainerRegistry containerRegistry, string name = null)
{
containerRegistry.RegisterForNavigation<TView>(name);
}

/// <summary>
/// Registers an object to be used as a dialog in the IDialogService.
/// </summary>
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; }
}
}