Skip to content

Commit

Permalink
TelerikWindowManager - proper sizing of content in Window
Browse files Browse the repository at this point in the history
If the view shown by WindowManager is a UserControl, size definitions
(Width/Height and also Min/Max values) will be tranferred to the hosting
RadWindow.
This makes resizing of a dialog to work properly, stretching the content
of the view.
  • Loading branch information
vcaraulean committed Nov 26, 2012
1 parent c13b4f0 commit d3e4a12
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
35 changes: 35 additions & 0 deletions WPF/Caliburn.Micro.Telerik/TelerikWindowManager.cs
Expand Up @@ -116,12 +116,18 @@ protected virtual RadWindow EnsureRadWindow(object model, object view, bool isDi

if (window == null)
{
var contentElement = view as FrameworkElement;
if (contentElement == null)
throw new ArgumentNullException("view");

window = new RadWindow
{
Content = view,
SizeToContent = true,
};

AdjustWindowAndContentSize(window, contentElement);

window.SetValue(View.IsGeneratedProperty, true);

var owner = GetActiveWindow();
Expand All @@ -147,6 +153,35 @@ protected virtual RadWindow EnsureRadWindow(object model, object view, bool isDi
return window;
}

/// <summary>
/// Initializes Window size with values extracted by the view.
///
/// Note:
/// The real size of the content will be smaller than provided values.
/// The form has the header (title) and border so they will take place.
///
/// </summary>
/// <param name="window">The RadWindow</param>
/// <param name="view">The view</param>
private static void AdjustWindowAndContentSize(RadWindow window, FrameworkElement view)
{
window.MinWidth = view.MinWidth;
window.MaxWidth = view.MaxWidth;
window.Width = view.Width;
window.MinHeight = view.MinHeight;
window.MaxHeight = view.MaxHeight;
window.Height = view.Height;

// Resetting view's settings
view.Width = view.Height = Double.NaN;
view.MinWidth = view.MinHeight = 0;
view.MaxWidth = view.MaxHeight = int.MaxValue;

// Stretching content to the Window
view.VerticalAlignment = VerticalAlignment.Stretch;
view.HorizontalAlignment = HorizontalAlignment.Stretch;
}

/// <summary>
/// Infers the owner of the window.
/// </summary>
Expand Down
58 changes: 30 additions & 28 deletions WPF/Examples.RadWindowManager/DialogView.xaml
@@ -1,28 +1,30 @@
<UserControl x:Class="Examples.TelerikWindowManager.DialogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
Height="400"
Width="600">

<Grid x:Name="LayoutRoot"
Background="White">

<TextBlock Text="Dialog view"
FontSize="26"
Margin="10" />

<telerik:RadButton Content="Close"
x:Name="Close"
Width="100"
Height="26"
Margin="20"
VerticalAlignment="Bottom"
HorizontalAlignment="Right" />
</Grid>
</UserControl>
<UserControl x:Class="Examples.TelerikWindowManager.DialogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
MinHeight="200"
Height="300"
MinWidth="300"
Width="400">

<Grid x:Name="LayoutRoot"
Background="White">

<TextBlock Text="Dialog view"
FontSize="26"
Margin="10" />

<telerik:RadButton Content="Close"
x:Name="Close"
Width="100"
Height="26"
Margin="20"
VerticalAlignment="Bottom"
HorizontalAlignment="Right" />
</Grid>
</UserControl>

0 comments on commit d3e4a12

Please sign in to comment.