【发布时间】:2009-09-25 12:48:05
【问题描述】:
我可以通过创建一个普通的 C# 类来继承和增强 ToolBar 类,然后这样做:
public class NiceToolBar : ToolBar
{
private ToolBarTray mainToolBarTray;
public NiceToolBar()
{
mainToolBarTray = new ToolBarTray();
mainToolBarTray.IsLocked = true;
this.Background = new SolidColorBrush(Colors.LightGray);
...
但这迫使我像这样操作 代码 中的所有控件:
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);
我真正需要的是 XAML 来完成这个繁琐的参数分配和布局。
所以我创建了一个新的用户控件,并将后面的代码更改为继承 ToolBar,如下所示:
public partial class SmartToolBar : ToolBar
{
public SmartToolBar(string label)
{
InitializeComponent();
TheLabel.Text = label;
}
}
在我的 XAML 中我放了这个:
<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>
但是当我运行它时,我得到了错误:
部分声明 “TestCallConstructor.Helpers.SmartToolBar” 不能定义不同的基类
我怎样才能让我的用户控制使用 XAML?
【问题讨论】:
标签: c# wpf xaml user-controls