I've created a custom control (my first) from the base class UserControl and now I wan't to be able to inherit from it when making future controls.
Here is the custom control template
<Style TargetType="{x:Type local:LoadOverlayUserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LoadOverlayUserControl}">
<Grid>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"/>
<Border Visibility="{TemplateBinding OverlayVisibility}"
Background="{TemplateBinding OverlayColor}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<dxe:ProgressBarEdit />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Here is the source code:
public class LoadOverlayUserControl : UserControl
{
static LoadOverlayUserControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LoadOverlayUserControl), new FrameworkPropertyMetadata(typeof(LoadOverlayUserControl)));
}
#region Overlay Visibility
public static readonly DependencyProperty OverlayVisibilityProperty = DependencyProperty.Register("OverlayVisibility",
typeof(Visibility),
typeof(LoadOverlayUserControl),
new PropertyMetadata(Visibility.Hidden));
private Visibility OverlayVisibility
{
get
{
return (Visibility)this.GetValue(OverlayVisibilityProperty);
}
set
{
this.SetValue(OverlayVisibilityProperty, value);
}
}
#endregion
#region Overlay Color
public static readonly DependencyProperty OverlayColorProperty = DependencyProperty.Register("OverlayColor",
typeof(Color),
typeof(LoadOverlayUserControl),
new PropertyMetadata(Color.FromRgb(0,0,0)));
public Color OverlayColor
{
get
{
return (Color)this.GetValue(OverlayColorProperty);
}
set
{
this.SetValue(OverlayColorProperty, value);
}
}
#endregion
public void SetLoading(bool IsLoading)
{
if (IsLoading)
{
OverlayVisibility = System.Windows.Visibility.Visible;
}
else
{
OverlayVisibility = System.Windows.Visibility.Hidden;
}
}
}
The problem I've run into is that when I inherit from this custom control, it seems to ignore my template and I end up with an empty control.
Here is an example of the new control that is inheriting from this custom control.
<custom:LoadOverlayUserControl x:Class "ChildControl"
...
...
...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../ResourceDictionaries/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
...
</Grid>
</custom:LoadOverlayUserControl>
And here is how I am using the new control. I've omitted namespaces for readability but I assure you they are correct unless there is something tricky I need to do.
<UserControl x:Class "SomeOtherControl">
<Grid>
<StackPanel>
<my:ChildControl />
</StackPanel>
</Grid>
</UserControl>
The idea behind the custom control is for controls that I want to be able to apply a "Currently Loading" overlay on top of with semi-transparent opacity.