简体   繁体   English

从Silverlight XAML调用基类方法

[英]Calling a base-class method from Silverlight XAML

As part of learning Silverlight, I'm trying to create a base UserControl to use as the starting point for my inherited controls. 作为学习Silverlight的一部分,我试图创建一个基本的UserControl用作我继承的控件的起点。 It's very simple, it merely defines some callback methods: 这很简单,它仅定义了一些回调方法:

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();
        }
    }

}

Please note the ClickToEditTextBox_KeyDown() method which is the problem! 请注意这是问题的ClickToEditTextBox_KeyDown()方法!

Now, I have an inherited control that looks as follows (CheckboxLayoutItem.xaml): 现在,我有一个继承的控件,看起来如下(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>

(Note - I have removed some namespace declarations for easier reading) Note the following line: (注意-为了方便阅读,我删除了一些名称空间声明)请注意以下行:

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

in which I set the KeyDown-event on a ClickToEditTextBox (the self namespace is defined, and correctly so). 在其中,我在ClickToEditTextBox上设置了KeyDown事件(定义了self命名空间,正确的是如此)。

Now, in the code behind (CheckboxLayoutItem.xaml.cs) in the constructor the call to InitializeComponent() fails with the error: Failed to assign to property 'System.Windows.UIElement.KeyDown'. 现在,在构造函数中(CheckboxLayoutItem.xaml.cs)后面的代码中,对InitializeComponent()的调用失败,并显示以下错误:无法分配给属性'System.Windows.UIElement.KeyDown'。 [Line: 17 Position: 42] [线:17位置:42]

I can't debug into InitializeComponent, however, but I can't see what could possibly be the issue from this error, other than the KeyDown events in the XAML. 但是,我无法调试InitializeComponent,但是除了XAML中的KeyDown事件之外,我看不到此错误可能是什么问题。

Now, here is my question - how come I (seemingly) cannot reference a method defined in my base-class!? 现在,这是我的问题-我(似乎)为什么不能引用基类中定义的方法! Previously I had the method in the CheckboxLayoutItem.xaml.cs method itself, but as some other controls needed some of the same functionality, it seemd a better option to put it in a base class. 以前,我在CheckboxLayoutItem.xaml.cs方法中拥有该方法,但是由于其他一些控件需要一些相同的功能,因此将其放在基类中似乎是一个更好的选择。

Cheers! 干杯!

I know this doesn't really answer your question, but you might want to look at Template (Custom Controls) controls. 我知道这并不能真正回答您的问题,但是您可能希望查看模板(自定义控件)控件。 UserControl really isn't the best solution for what you're trying to do here. 对于您要在此处执行的操作,UserControl确实不是最佳解决方案。

UserControls are best for situations where you're building a one-off control that you don't intend in inheriting from. UserControls最适合您要构建不打算从中继承的一次性控件的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM