How do you create a base class for a user control?
I think I need to create a new base class for my controls, because what I am trying to do is create multiple controls for various layout variations but I want them to all process the same information.
It's for an application where I initialize a new usercontrol and pass it parameters in the constructor, for it to add buttons to the instance of the control with a specific layout to it.
Do I need binding in the xaml file so that the base class can control what's on the xaml file?
Your best bet would be to use a Custom rather than User control. This is because custom controls are look less which would allow you to use different styles for the different layouts.
The other option would be to use inheritance.
eg given a base class called Foo
public Foo : UserControl
{
...
}
you would then create multiple controls extending this
code:
public partial Bar : Foo
{
...
}
XAML:
<Foo xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid >
</Grid>
</Foo>
(Note the use of the base class instead of UserControl in the Xaml.)
精彩评论