从 WPF 中的 UserControl 继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/269106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:45:48  来源:igfitidea点击:

Inheriting from a UserControl in WPF

wpfinheritanceuser-controls

提问by user35129

I am fairly new to WPF and I am having a problem with inheriting from a user control.

我对 WPF 还很陌生,我在从用户控件继承时遇到了问题。

I created a User Control and now I need to inherit from that control and add some more functionality.

我创建了一个用户控件,现在我需要从该控件继承并添加更多功能。

Has anyone does this sort of thing before? Any help would be greatly appreciated.

以前有人做过这种事情吗?任何帮助将不胜感激。

Thank you

谢谢

采纳答案by BFreeman

AFAIK you cannot inherit the xaml, you can only inherit the code behind.

AFAIK 你不能继承 xaml,你只能继承后面的代码。

We recently encountered the same problem on our project. The way we ended up solving our problem was to create a usercontrol and adding it to the "child" usercontrol.

我们最近在我们的项目中遇到了同样的问题。我们最终解决问题的方法是创建一个用户控件并将其添加到“子”用户控件中。

If that doesnt work/help take a look at this: http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx1

如果那不起作用/帮助看看这个:http: //geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx 1

回答by sirrocco

Well .. you create your base control

好吧..你创建了你的基本控件

public abstract class BaseUserControl : UserControl{...}

then in the XAML file :

然后在 XAML 文件中:

<Controls:BaseUserControl x:Class="Termo.Win.Controls.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Namespace.Of.Your.BaseControl">

And that should work.

这应该有效。

EDIT:Hmm.. this example is useful when you have a base control without XAML and then inherit from it. The other way around(from a base control with Xaml) - I'm not sure how you can go about it.

编辑:嗯.. 当您有一个没有 XAML 的基本控件然后从它继承时,这个示例很有用。反过来(从带有 Xaml 的基本控件) - 我不知道你可以怎么做。

EDIT2:Apparently from this post + commentsi take that what you want might not be possible.

EDIT2:显然从这篇文章+评论我认为你想要的可能是不可能的。

回答by Tomá? Kafka

I may have a bit of a solution: Composition instead of inheritance - I have come up with control, that has 'content slots' assignable from outside through databinding, look at my SO thread.

我可能有一些解决方案:组合而不是继承 - 我提出了控制,它具有可通过数据绑定从外部分配的“内容槽”,看看我的SO 线程

Example of use:

使用示例:

<UserControl ... >

    <!-- My wrapping XAML -->
        <Common:DialogControl>
                <Common:DialogControl.Heading>
                        <!-- Slot for a string -->
                </Common:DialogControl.Heading>
                <Common:DialogControl.Control>
                        <!-- Concrete dialog's content goes here -->
                </Common:DialogControl.Control>
                <Common:DialogControl.Buttons>
                        <!-- Concrete dialog's buttons go here -->
                </Common:DialogControl.Buttons>
        </Common:DialogControl>
    <!-- /My wrapping XAML -->

</UserControl>

Together with some handling code in codebehind it would be a nice base component for dialog windows.

与代码隐藏中的一些处理代码一起,它将成为对话框窗口的一个很好的基础组件。

回答by user1005465

You cannot inherit the xaml code it self. However creating an abstract class of the codebehind, will allow you to edit in code behind, from a derived class object.

您不能自行继承 xaml 代码。但是,创建代码隐藏的抽象类将允许您从派生类对象在代码隐藏中进行编辑。

Xaml Code: { Window1.xaml }

Xaml 代码:{ Window1.xaml }

<Window 
x:Class="WPFSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="auto" Width="256" Title="WPF Sameples">
  <Grid>
    <Button x:Name="Button1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Click Me"/>
  </Grid>
</Window>

CodeBehind: { Window1.xaml.cs }

代码隐藏:{ Window1.xaml.cs }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFSamples
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public abstract partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

Derived Class : { DisabledButtonWindow.cs }

派生类:{DisabledButtonWindow.cs}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WPFSamples
{
    public sealed class DisabledButtonWindow : Window1
    {
        public DisabledButtonWindow()
        {
            Button1.IsEnabled = false;
        }
    }
}

although you cannot inherit from the wpf source it self, you are able to use this "Window1" control as a template for all other derived controls.

虽然您不能从 wpf 源继承它自己,但您可以使用这个“Window1”控件作为所有其他派生控件的模板。

回答by Matthew Beatty

I think that you can do this but that you will have to redefine any functions and possibly some other stuff that you reference in the xaml in the child class.

我认为您可以做到这一点,但是您必须重新定义任何函数以及您在子类的 xaml 中引用的其他一些内容。

IE if you have a button click event that you subscribe to in the base class xaml you will need override the button click in the child class and call the base class button click event.

IE 如果您有一个在基类 xaml 中订阅的按钮单击事件,您将需要覆盖子类中的按钮单击并调用基类按钮单击事件。

Not one hundred percent sure of the the details since it's my coworkers code that I'm drawing from but thought this would give a start to anyone looking to implement this in the future.

不能百分百确定细节,因为这是我正在绘制的同事代码,但我认为这将为任何希望在未来实现这一点的人提供一个开始。

回答by Maximiliano

You can accomplish this by using a delegate.

您可以通过使用delegate.

Essentially, you need to create an interface (YourInterface) which wraps up the functionality you want, then make both the user control and your class implement that interface.

本质上,您需要创建一个接口 ( YourInterface) 来封装您想要的功能,然后让用户控件和您的类都实现该接口。

Next, make sure the user control has a reference to an object of type IYourInterface, so that when your user control attempts to invoke a method of the Interface, it calls your class' method.

接下来,确保用户控件具有对 类型对象的引用IYourInterface,以便当您的用户控件尝试调用 的方法时Interface,它会调用您的类的方法。

Because both the user control and class implement the same interface, they can be seen as the same kind of object - meaning you can put them both into a collection of objects of type IYourInterface. This should give you the behavior you want.

因为用户控件和类都实现了相同的接口,所以可以将它们视为同一种对象——这意味着您可以将它们都放入类型为 的对象集合中IYourInterface。这应该给你你想要的行为。

In ASP.NET I use this technique often, by having my classes inherit from abstract classancestors. I don't understand why WPF doesn't support this. :(

在 ASP.NET 中,我经常使用这种技术,让我的类从abstract class祖先那里继承。我不明白为什么 WPF 不支持这一点。:(