从Silverlight XAML调用基类方法

时间:2010-11-26 13:12:27

标签: c# silverlight xaml silverlight-4.0

作为学习Silverlight的一部分,我正在尝试创建一个基本的UserControl,作为我继承控件的起点。 它非常简单,它只定义了一些回调方法:

public class ClickableUserControl : UserControl
{
    private Control _superParent;

    public ClickableUserControl()
    {

    }

    public ClickableUserControl(Control superParent)
    {
        _superParent = superParent;

        this.MouseEnter += new MouseEventHandler(PostfixedLayoutItem_MouseEnter);
        this.MouseLeave += new MouseEventHandler(PostfixedLayoutItem_MouseLeave);
        this.MouseLeftButtonDown += new MouseButtonEventHandler(PostfixedLayoutItem_MouseLeftButtonDown);
    }

    public virtual void PostfixedLayoutItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var elements = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), this);
        if (elements.Any(elm => elm is ClickToEditTextBox))
        {
            e.Handled = false;
        }
    }

    public void PostfixedLayoutItem_MouseLeave(object sender, MouseEventArgs e)
    {
        this.Cursor = Cursors.Arrow;
    }

    public void PostfixedLayoutItem_MouseEnter(object sender, MouseEventArgs e)
    {
        this.Cursor = Cursors.Hand;
    }

    public void ClickToEditTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter || e.Key == Key.Escape)
        {
            VisualStateManager.GoToState((Control)sender, "NotEdit", false);
            _superParent.Focus();
        }
    }

}

请注意ClickToEditTextBox_KeyDown()方法就是问题!

现在,我有一个继承的控件,如下所示(CheckboxLayoutItem.xaml):

<local:ClickableUserControl x:Class="OpusFormBuilder.LayoutItems.CheckboxLayoutItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:OpusFormBuilder.LayoutItems"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" x:Name="LayoutItem">
<StackPanel Name="stackPanel1" Orientation="Horizontal">
    <lc:LayoutItem Label="layoutItem" Name="layoutItem">
                <lc:LayoutItem.LabelTemplate>
                    <DataTemplate>
                        <Self:ClickToEditTextBox KeyDown="ClickToEditTextBox_KeyDown" Text="{Binding Label, Mode=TwoWay,  ElementName=layoutItem}" MaxWidth="150" MinWidth="150" TextWrapping="Wrap" MaxHeight="200" VerticalAlignment="Top" />
                    </DataTemplate>
                </lc:LayoutItem.LabelTemplate>
                <dxe:CheckEdit Name="InnerCheckbox" Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Stretch" IsEnabled="False" />
            </lc:LayoutItem>
    <Self:ClickToEditTextBox KeyDown="ClickToEditTextBox_KeyDown" x:Name="Description" MaxWidth="150" MaxHeight="200" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Right" />
</StackPanel>

(注意 - 我删除了一些名称空间声明以便于阅读) 请注意以下行:

<Self:ClickToEditTextBox KeyDown="ClickToEditTextBox_KeyDown" Text="{Binding Label, Mode=TwoWay,  ElementName=layoutItem}" MaxWidth="150" MinWidth="150" TextWrapping="Wrap" MaxHeight="200" VerticalAlignment="Top" />

我在ClickToEditTextBox上设置KeyDown事件(定义了自命名空间,并且正确如此)。

现在,在构造函数后面的代码(CheckboxLayoutItem.xaml.cs)中,对InitializeComponent()的调用失败并显示错误:无法分配给属性'System.Windows.UIElement.KeyDown'。 [线路:17位置:42]

然而,我无法调试到InitializeComponent,但除了XAML中的KeyDown事件之外,我无法看到此错误可能出现的问题。

现在,这是我的问题 - 为什么我(貌似)不能引用我的基类中定义的方法!?以前我在CheckboxLayoutItem.xaml.cs方法本身中使用了该方法,但由于其他一些控件需要一些相同的功能,所以将它放在基类中似乎是更好的选择。

干杯!

1 个答案:

答案 0 :(得分:1)

我知道这并没有真正回答你的问题,但你可能想看看模板(自定义控件)控件。 UserControl确实不是你想要做的最好的解决方案。

UserControls最适用于您正在构建一个您不打算继承的一次性控件的情况。