XAML继承(不包含代码) [英] XAML Inheritance (not code-behind)

查看:163
本文介绍了XAML继承(不包含代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 XAML 中实现继承?

是真的,只有后面的代码可以被继承,而不是 .xaml (相关问题)?唯一的方法是将父级包含在子级控件的命名空间中吗?

Is it true that only the code-behind can be inherited and not the .xaml (related question)? Is including the parent in the child control's namespace the only way of doing it?

XAML 本身似乎没有一般的继承".关于继承UserControl等有问题,但没有关于一般继承的问题.

There seems to be no general "inheritance" per se in XAML. There are questions about inheriting UserControl, etc. but not general inheritance.

问题:

我有2个xaml文件,分别是File1.xamlFile2.xaml.我可以创建一个SuperFile.xaml并将下面的大部分代码放入其中吗?

I have 2 xaml files, File1.xaml and File2.xaml, which are very alike. Can I create a SuperFile.xaml and put the bulk of the following code in it?

<UserControl ... >
    <Grid>
        <Grid.RowDefinitions ... />

        <DockPanel ... >
            <ToolBar ... >
                <!-- Some Buttons here:
                     File1.xaml to contain the Buttons "View" and "Export"
                     File2.xaml to contain the Buttons "Edit" and "Delete"
                 -->
            </ToolBar>
        </DockPanel>

        <igDP:XamDataGrid MouseDoubleClick="xamDataGrid1_MouseDoubleClick" ... />
    </Grid>
</UserControl>

File1.xamlFile2.xaml中唯一与不同的是:

    ToolBar中的
  1. Button(请参见代码中的注释)
  2. XamDataGrid属性,主要是事件(它们在每种情况下的作用).
  1. Buttons in the ToolBar (see comment in the code)
  2. The properties of XamDataGrid, primarily the events (what they do in each case).

我该如何实现?我是否需要分别分别为两个孩子编写代码隐藏文件?

How can I achieve this? Would I have to write the code-behind files for both the children separately?

推荐答案

您可以使用资源字典

将所有通用模板和样式放在ResourceDictionary中,然后让UserControls导入该ResourceDictionary

Put all your generic templates and styles in a ResourceDictionary, then have both your UserControls import that ResourceDictionary

<UserControl.Resources>
    <ResourceDictionary Source="BaseStylesAndTemplates.xaml" />
</UserControl.Resources>

这篇关于XAML继承(不包含代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆