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...