I am pretty new to WPF, (using it for 3 weeks now), so I might be missing something stupid or not understanding what I am doing!
I am trying to create a modal type popup window that would cover either the whole application or current control it is in. I want this control to be semi transparent so the user can still see the content behind but not be able to use it. They will then be able to complete what ever is in the modal window before continuing.
I do not want to have to repeat code in different places so my goal is to have a generic control that I can use in my XAML and only have to add the content I need each time. I.e. the fading, transparency, background colour extra are all handled in one place and I only have to add the specific functionality for that instance of it.
So far I have created a usercontrol called jonblind:
<UserControl x:Class="ShapInteractiveClient.View.SampleTests.jonblind"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="blindGrid" Grid.RowSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Opacity="0.82">
<Grid.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0" MappingMode="RelativeToBoundingBox">
<GradientStop Color="#FFA8CBFE" Offset="1"/>
<GradientStop Color="Red"/>
<GradientStop Color="#FFE1EDFE" Offset="0.147"/>
</LinearGradientBrush>
</Grid.Background>
<ContentControl x:Name="contentTemplateArea" />
</Grid>
</UserControl>
I have a code behind for the control as follows:
public partial class jonblind : UserControl
{
public jonblind()
{
InitializeComponent();
SetVisibility(this);
}
[Category("jonblind")]
public bool IsContentVisible
{
get { return (bool)GetValue(IsContentVisibleProperty); }
set { SetValue(IsContentVisibleProperty, value); }
}
public static readonly DependencyProperty IsContentVisibleProperty = DependencyProperty.Register("IsContentVisible", typeof(bool), typeof(jonblind),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsOverlayContentVisibleChanged)));
private static void OnIsOverlayContentVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
jonblind blind = d as jonblind;
if(blind != null)
SetVisibility(blind);
}
private static void SetVisibility(jonblind blind)
{
blind.blindGrid.Visibility = blind.IsContentVisible ? Visibility.Visible : Visibility.Hidden;
}
[Category("jonblind")]
public ContentControl ContentAreaControl
{
get { return (ContentControl)GetValue(ContentAreaControlProperty); }
set { SetValue(ContentAreaControlProperty, value); }
}
public static readonly DependencyProperty ContentAreaControlProperty = DependencyProperty.Register("ContentAreaControl", typeof(ContentControl), typeof(jonblind),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnContentAreaControlChanged)));
private static void OnContentAreaControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
jonblind blind = d as jonblind;
if (blind != null && e.NewValue != null && e.NewValue is ContentControl)
{
blind.contentTemplateArea = e.NewValue as ContentControl;
}
}
}
I can add it to another usercontrol as follows:
<UserControl.Resources>
<ContentControl x:Key="testcontrol">
<StackPanel>
<TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Text="Loading!!!" />
<Button Content="hide me!" Command="{Binding Path=alternateblind}" />
</StackPanel>
</ContentControl>
</UserControl.Resources>
<SampleTests:jonblind IsContentVisible="{Binding Path=ShowBlind}" ContentAreaControl="{StaticResource testcontrol}" />
If I put a break point on "OnContentAreaControlChanged" I can see the new content being passed in but it will never display it at runtime.
I don't know if I am going about this all wrong, if it is even possible or if it just needs tweeking. Any and all advice on this and dealing with this kind of scenario would be greatly appreciated.