从 Silverlight XAML 调用基类方法

发布于 2024-10-05 03:25:06 字数 3684 浏览 3 评论 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 事件(已定义 self 命名空间,并且正确如此)。

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

但是,我无法调试到 InitializeComponent,但除了 XAML 中的 KeyDown 事件之外,我看不出此错误可能存在什么问题。

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

干杯!

As part of learning Silverlight, I'm trying to create a base UserControl to use as the starting point for my inherited controls.
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!

Now, I have an inherited control that looks as follows (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).

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'. [Line: 17 Position: 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.

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.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2024-10-12 03:25:06

我知道这并不能真正回答您的问题,但您可能想查看模板(自定义控件)控件。 UserControl 实际上并不是您在这里尝试执行的操作的最佳解决方案。

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

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.

UserControls are best for situations where you're building a one-off control that you don't intend in inheriting from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文