WPF用户控件继承问题

用户名

我在一个图书馆项目中做了一个WPF控件,并想用一个新的控件对其进行扩展。

<UserControl x:Class="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI.EmbeddingUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

    </Grid>
</UserControl>

我试图像这样扩展它:

<src:EmbeddingUserControl x:Class="Interaxalab.Desktop.Modules.PrototipoCable.CustomViews.InteractionView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI"
    Name="InteractionWorksheetView" Height="321.613" Width="471.396"
>
    <Grid>

        <WindowsFormsHost x:Name="windowsFormsHost1" HorizontalAlignment="Left" Height="284" VerticalAlignment="Top" Width="471"/>

    </Grid>
</src:EmbeddingUserControl>

但是,我收到一条错误消息,指出名称空间“ Genesyslab.Desktop.Modules.ExtensionUtils85.GUI”中不存在名称“ EmbeddingUserControl”。

该名称显然确实存在,因为xaml.cs可以找到它,但是由于某种原因xaml找不到。

任何帮助,将不胜感激。

EVK

长话短说-您不能通过xaml的另一个控件继承xaml的控件(这样做是否有意义?)。在您的情况下,EmbeddingUserControl不包含任何可视树元素(只是空网格),因此您可以执行以下操作:

public class EmbeddingUserControl : UserControl {
    // some methods, properties of your control
}

然后,你可以继承就像你在你的问题已经做(不从EmbeddingUserControl忘记继承在XAML文件和代码隐藏文件)。

如果您继承的控件本身没有xaml,则还可以使用xaml从用户控件继承(因此您可以添加\ override逻辑)。

如果您需要继承一些视觉结构,则必须从继承切换为合成。即:您的基本控件提供了一些可以放置其他控件的占位符,甚至更好地允许提供用于格式化数据项的模板(例如ItemsControl和ItemTemplate属性)。然后,您仅在必要时用其他控件填充这些占位符。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章