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

Let the BaseMetroDialog inherit from ContentControl instead of Control #1615

Merged
merged 1 commit into from
Oct 27, 2014
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
37 changes: 1 addition & 36 deletions MahApps.Metro/Controls/Dialogs/BaseMetroDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,9 @@ namespace MahApps.Metro.Controls.Dialogs
/// You probably don't want to use this class, if you want to add arbitrary content to your dialog,
/// use the <see cref="SimpleDialog"/> class.
/// </summary>
[System.Windows.Markup.ContentProperty("DialogBody")]
public abstract class BaseMetroDialog : Control
public abstract class BaseMetroDialog : ContentControl
{
private const string PART_DialogBody_ContentPresenter = "PART_DialogBody_ContentPresenter";
protected ContentPresenter DialogBody_ContentPresenter = null;

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(BaseMetroDialog), new PropertyMetadata(default(string)));
public static readonly DependencyProperty DialogBodyProperty = DependencyProperty.Register("DialogBody", typeof(object), typeof(BaseMetroDialog), new PropertyMetadata(null, (o, e) =>
{
var dialog = o as BaseMetroDialog;
if (dialog != null)
{
if (e.OldValue != null)
{
dialog.RemoveLogicalChild(e.OldValue);
}
if (e.NewValue != null)
{
dialog.AddLogicalChild(e.NewValue);
}
}
}));
public static readonly DependencyProperty DialogTopProperty = DependencyProperty.Register("DialogTop", typeof(object), typeof(BaseMetroDialog), new PropertyMetadata(null));
public static readonly DependencyProperty DialogBottomProperty = DependencyProperty.Register("DialogBottom", typeof(object), typeof(BaseMetroDialog), new PropertyMetadata(null));

Expand All @@ -52,15 +33,6 @@ public string Title
set { SetValue(TitleProperty, value); }
}

/// <summary>
/// Gets/sets arbitrary content in the "message" area in the dialog.
/// </summary>
public object DialogBody
{
get { return GetValue(DialogBodyProperty); }
set { SetValue(DialogBodyProperty, value); }
}

/// <summary>
/// Gets/sets arbitrary content on top of the dialog.
/// </summary>
Expand All @@ -87,13 +59,6 @@ static BaseMetroDialog()
DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseMetroDialog), new FrameworkPropertyMetadata(typeof(BaseMetroDialog)));
}

public override void OnApplyTemplate()
{
DialogBody_ContentPresenter = GetTemplateChild(PART_DialogBody_ContentPresenter) as ContentPresenter;

base.OnApplyTemplate();
}

/// <summary>
/// Initializes a new MahApps.Metro.Controls.BaseMetroDialog.
/// </summary>
Expand Down
14 changes: 1 addition & 13 deletions MahApps.Metro/Controls/Dialogs/SimpleDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ namespace MahApps.Metro.Controls.Dialogs
/// <summary>
/// A simple implementation of BaseMetroDialog allowing arbitrary content.
/// </summary>
[ContentProperty("DialogBody")]
public class SimpleDialog : BaseMetroDialog
{
public SimpleDialog()
{
this.Loaded += SimpleDialog_Loaded;
}

void SimpleDialog_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
if (DialogBody_ContentPresenter.Content != null) //temp fix for #1238. it forces bindings to bind since for some reason, they won't when the dialog is shown.
((FrameworkElement)DialogBody_ContentPresenter.Content).InvalidateProperty(FrameworkElement.DataContextProperty);
}
}
{ }
}
53 changes: 53 additions & 0 deletions MahApps.Metro/Controls/TreeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,59 @@ public static T TryFindParent<T>(this DependencyObject child)
return parent ?? TryFindParent<T>(parentObject);
}

/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
public static T FindChild<T>(this DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;

T foundChild = null;

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);

// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}

return foundChild;
}

/// <summary>
/// This method is an alternative to WPF's
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
Expand Down
6 changes: 2 additions & 4 deletions MahApps.Metro/Themes/Dialogs/BaseMetroDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@
Foreground="{TemplateBinding Foreground}"
Text="{TemplateBinding Title}"
TextWrapping="Wrap" />
<ContentPresenter x:Name="PART_DialogBody_ContentPresenter"
Grid.Row="1"
Content="{TemplateBinding DialogBody}"
DataContext="{TemplateBinding DataContext}" />
<ContentPresenter Grid.Row="1"
Content="{TemplateBinding Content}" />
</Grid>
</Grid>
<ContentPresenter Grid.Row="2"
Expand Down
78 changes: 38 additions & 40 deletions MahApps.Metro/Themes/Dialogs/InputDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,43 @@
xmlns:controls="clr-namespace:MahApps.Metro.Controls"
x:Class="MahApps.Metro.Controls.Dialogs.InputDialog"
Loaded="Dialog_Loaded">
<Dialogs:BaseMetroDialog.DialogBody>
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="20" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
Text="{Binding Message, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Grid.Row="1"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
controls:TextboxHelper.FocusBorderBrush="{DynamicResource AccentColorBrush}"
x:Name="PART_TextBox"
Text="{Binding Input, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="20" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
Text="{Binding Message, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Grid.Row="1"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
controls:TextboxHelper.FocusBorderBrush="{DynamicResource AccentColorBrush}"
x:Name="PART_TextBox"
Text="{Binding Input, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />

<StackPanel Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Right"
Height="85">
<Button x:Name="PART_AffirmativeButton"
Height="35"
MinWidth="80"
Style="{DynamicResource AccentedDialogSquareButton}"
Content="{Binding AffirmativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="0 0 5 0" />
<Button x:Name="PART_NegativeButton"
Height="35"
MinWidth="80"
Content="{Binding NegativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="5 0 5 0" />
</StackPanel>
</Grid>
</Dialogs:BaseMetroDialog.DialogBody>
<StackPanel Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Right"
Height="85">
<Button x:Name="PART_AffirmativeButton"
Height="35"
MinWidth="80"
Style="{DynamicResource AccentedDialogSquareButton}"
Content="{Binding AffirmativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="0 0 5 0" />
<Button x:Name="PART_NegativeButton"
Height="35"
MinWidth="80"
Content="{Binding NegativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:InputDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="5 0 5 0" />
</StackPanel>
</Grid>
</Dialogs:BaseMetroDialog>
96 changes: 46 additions & 50 deletions MahApps.Metro/Themes/Dialogs/LoginDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,50 @@
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs"
x:Class="MahApps.Metro.Controls.Dialogs.LoginDialog"
Loaded="Dialog_Loaded">

<Dialogs:BaseMetroDialog.DialogBody>
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="20" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
Text="{Binding Message, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Grid.Row="1"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
x:Name="PART_TextBox"
Controls:TextboxHelper.Watermark="{Binding UsernameWatermark, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Username, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<PasswordBox Grid.Row="2"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
x:Name="PART_TextBox2"
Controls:TextboxHelper.Watermark="{Binding PasswordWatermark, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel Grid.Row="3"
Orientation="Horizontal"
HorizontalAlignment="Right"
Height="85">
<Button x:Name="PART_AffirmativeButton"
Height="35"
MinWidth="80"
Style="{DynamicResource AccentedDialogSquareButton}"
Content="{Binding AffirmativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="0 0 5 0" />
<Button x:Name="PART_NegativeButton"
Height="35"
MinWidth="80"
Visibility="{Binding NegativeButtonButtonVisibility, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Content="{Binding NegativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="5 0 5 0" />
</StackPanel>
</Grid>
</Dialogs:BaseMetroDialog.DialogBody>

<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
MinHeight="20" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
Text="{Binding Message, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Grid.Row="1"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
x:Name="PART_TextBox"
Controls:TextboxHelper.Watermark="{Binding UsernameWatermark, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Username, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<PasswordBox Grid.Row="2"
Margin="0 5 0 0"
FontSize="{StaticResource DialogMessageFontSize}"
x:Name="PART_TextBox2"
Controls:TextboxHelper.Watermark="{Binding PasswordWatermark, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel Grid.Row="3"
Orientation="Horizontal"
HorizontalAlignment="Right"
Height="85">
<Button x:Name="PART_AffirmativeButton"
Height="35"
MinWidth="80"
Style="{DynamicResource AccentedDialogSquareButton}"
Content="{Binding AffirmativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="0 0 5 0" />
<Button x:Name="PART_NegativeButton"
Height="35"
MinWidth="80"
Visibility="{Binding NegativeButtonButtonVisibility, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Content="{Binding NegativeButtonText, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
Margin="5 0 5 0" />
</StackPanel>
</Grid>
</Dialogs:BaseMetroDialog>
Loading