1

I have an assortment of user controls and I'm trying to see if I can create a base class for them with some dependency properties.

Specifically, most of my user controls follow this format...

<UserControl DataContext="{Binding MyDataContext}" >
    <Expander IsExpanded="{Binding MyExpandedByDefault}">
        <TextBlock>Some text</TextBlock>
    </Expander>
</UserControl>

Of course, normally if this was just a one-off, I'd write the dependency property in the code behind for the above user control. However, since I have multiple user controls that follow the same format, I'd like to put something like the following in a base class...

public bool ExpandedByDefault
{
    get { return (bool)GetValue(ExpandedByDefaultProperty); }
    set { SetValue(ExpandedByDefaultProperty, value); }
}

public static readonly DependencyProperty ExpandedByDefaultProperty =
    DependencyProperty.Register("ExpandedByDefault", typeof(bool), typeof(MyBaseView), new UIPropertyMetadata());

I would like for that to be inherited somewhere so in my main window I can do something like....

<Window>
    <StackPanel>
        <my:Alpha ExpandedByDefault="True" />
        <my:Bravo ExpandedByDefault="False" />
    </StackPanel>
</Window>

Thanks

EDIT:

I have made a base class like so...

public class ViewBase : UserControl
{
    public static readonly DependencyProperty ExpandedByDefaultProperty =
                 DependencyProperty.Register("ExpandedByDefault",
                                             typeof(bool),
                                             typeof(FiapaDbViewerBase),
                                             new FrameworkPropertyMetadata());

    public bool ExpandedByDefault
    {
        get
        {
            return (bool)this.GetValue(ExpandedByDefaultProperty);
        }
        set
        {
            this.SetValue(ExpandedByDefaultProperty, value);
        }
    }
}

But when I try to inherit it in the code behind for my user control like so....

public partial class MyUserControl : ViewBase
{
    public MyUserControl()
    {
        InitializeComponent();
    }
}

I get an error saying

Partial declarations of 'MyUserControl' must not specify different base classes

And I cannot find the other part of the partial class in my solution??? I've tried searching for it in the whole solution...

imdandman
  • 393
  • 2
  • 6
  • 15

1 Answers1

2

You can have inheritance. Like so:

  1. Define a base class:

    public class BaseExpanderUC : UserControl
    {
        public bool ExpandedByDefault
        {
            get { return (bool)GetValue(ExpandedByDefaultProperty); }
            set { SetValue(ExpandedByDefaultProperty, value); }
        }
    
        public static readonly DependencyProperty ExpandedByDefaultProperty =
            DependencyProperty.Register("ExpandedByDefault", typeof(bool), typeof(MyBaseView), new UIPropertyMetadata(false));
    }
    
  2. Define an inherited classes:

    public class Alpha : BaseExpanderUC{}
    public class Bravo : BaseExpanderUC{}
    
  3. In each of the XAMLs of each inherited classes (Alpha and Bravo above), use this makup:

    <BaseExpanderUC>
        <Expander IsExpanded="{Binding MyExpandedByDefault,
                                       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BaseExpanderUC}}}">
            <TextBlock>Some text</TextBlock>
        </Expander>
    </BaseExpanderUC>
    

    Where "local" is an xmlns for the namespace of BaseExpanderUC.

This will demand you to define the UI for each UC. If you can have a common UI for all controls, I'd strongly suggest that you use a custom control (probably inheriting Expander). Then you'll have to define the UI only once, in a ControlTemplate.

XAMeLi
  • 6,189
  • 2
  • 22
  • 29
  • Oops, copy-pasted your code, didn't change the root element in XAML. See my edit in clause 3. – XAMeLi Apr 09 '13 at 15:46
  • Thanks for your edit... but how does that fix the partial class error I'm getting? Thanks. – imdandman Apr 09 '13 at 16:29
  • Derp. Thanks. I missed the part where you Changed the opening tag to ``. I also noticed [this](http://stackoverflow.com/questions/887519/how-can-a-wpf-usercontrol-inherit-a-wpf-usercontrol) previously answered question too in case anyone needs it. – imdandman Apr 09 '13 at 16:42