I can inherit and enhance the ToolBar class by creating a plain C# class, then doing this:
public class NiceToolBar : ToolBar
{
private ToolBarTray mainToolBarTray;
public NiceToolBar()
{
mainToolBarTray = new ToolBarTray();
mainToolBarTray.IsLocked = true;
this.Background = new SolidColorBrush(Colors.LightGray);
...
But this forces me to manipulate all my controls in code like this:
ToolBar toolBar = new ToolBar();
toolBar.Background = new SolidColorBrush(Colors.Transparent);
toolBar.Cursor = Cursors.Hand;
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;
TextBlock tb = new TextBlock();
tb.Text = label;
tb.Margin = new Thickness { Top = 3, Left = 3, Bottom = 3, Right = 10 };
Image image = new Image();
image.Source = new BitmapImage(new Uri("Images/computer.png", UriKind.Relative));
sp.Children.Add(image);
sp.Children.Add(tb);
toolBar.Items.Add(sp);
What I really need is XAML to do this tedious parameter assigning and layout.
So I create a new User Control and change the code behind to inherit ToolBar like this:
public partial class SmartToolBar : ToolBar
{
public SmartToolBar(string label)
{
InitializeComponent();
TheLabel.Text = label;
}
}
And in my XAML I put this:
<UserControl x:Class="TestUserControl.Helpers.SmartToolBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal">
<Image Source="Images/computer.png"/>
<TextBlock x:Name="TheLabel"/>
</StackPanel>
</UserControl>
But when I run it, I get the error:
Partial declaration of "TestCallConstructor.Helpers.SmartToolBar" may not define different basis classes
How can I have my user control with XAML?