Reputation: 1129
I've created several UserControls which got some common Properties. Is it possible to create a base-UserControl from which the concrete ones can derive?
Base Class:
public class LabeledControlBase : UserControl {
public string ControlWidth {
get { return (string)GetValue(ControlWidthProperty); }
set { SetValue(ControlWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for ControlWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ControlWidthProperty =
DependencyProperty.Register("ControlWidth", typeof(string), typeof(LabeledControlBase), new PropertyMetadata("Auto"));
}
Concrete Class:
public partial class LabeledTextBox : Base.LabeledControlBase {
public string LabelText {
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
// Using a DependencyProperty as the backing store for LabelText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.Register("LabelText", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(null));
public string TextBoxText {
get { return (string)GetValue(TextBoxTextProperty); }
set { SetValue(TextBoxTextProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBoxText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBoxTextProperty =
DependencyProperty.Register("TextBoxText", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(null));
What I want to achive: I want to be able to set the "ControlWidth" Property on any UserControl which derives from "LabeledControlBase".
What is the problem: The way I realized it my LabeledTextBox does not recognize GetValue and SetValue Methods although it inherits from LabeledControlBase which derives from UserControl. When changing Base.LabeledControlBase to UserControl everything works just fine (except I can not access my commonly used Properties).
Upvotes: 3
Views: 927
Reputation: 5518
Since I haven't seen LabeledTextBox.xaml
I can't be sure, but I suspect your XAML is somehow wrong -- perhaps using UserControl
as the root? The XAML should look something like this:
<wpf:LabeledControlBase
x:Class="WPF.LabeledTextBox"
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"
xmlns:wpf="clr-namespace:WPF"
x:Name="self"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Text="{Binding ElementName=self, Path=ControlWidth}" />
</Grid>
</wpf:LabeledControlBase>
...with LabeledControlBase
as the root. This lets me use a LabeledTextBox
control in a page:
<wpf:LabeledTextBox ControlWidth="Hi" />
(ControlWidth
's intended semantics are likely not what I'm doing with it; this is just to verify that things work. And they do.)
Upvotes: 1