sorpha
sorpha

Reputation: 433

Inheriting From Custom Control

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.

Upvotes: 3

Views: 322

Answers (1)

Abe Heidebrecht
Abe Heidebrecht

Reputation: 30498

When you want to create a custom control in WPF, you will want to do a few things differently.

First, inherit from ContentControl, not UserControl.

Second, your resource dictionary Generic.xaml should go under the Themes folder.

Third, your derived control classes will also need to be WPF controls. That is, they will need to override the default style key, and define their style in Generic.xaml.

Upvotes: 2

Related Questions