简体   繁体   English

XAML继承(不是代码隐藏)

[英]XAML Inheritance (not code-behind)

How is inheritance accomplished in XAML ? 如何在XAML中完成继承

Is it true that only the code-behind can be inherited and not the .xaml ( related question )? 是真的只有代码隐藏可以继承而不是.xaml相关问题 )吗? Is including the parent in the child control's namespace the only way of doing it? 在child控件的命名空间中包含父元素是唯一的方法吗?

There seems to be no general "inheritance" per se in XAML . XAML中似乎没有一般的“继承”。 There are questions about inheriting UserControl , etc. but not general inheritance . 有关继承UserControl等的问题 ,但不是一般继承

Problem: 问题:

I have 2 xaml files, File1.xaml and File2.xaml , which are very alike. 我有2个xaml文件, File1.xamlFile2.xaml ,它们非常相似。 Can I create a SuperFile.xaml and put the bulk of the following code in it? 我可以创建一个SuperFile.xaml并将大部分以下代码放入其中吗?

<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>

The only things that differ in File1.xaml and File2.xaml are: File1.xamlFile2.xaml中唯一不同的是:

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

How can I achieve this? 我怎样才能做到这一点? Would I have to write the code-behind files for both the children separately ? 我是否必须分别为两个孩子编写代码隐藏文件?

You can use a ResourceDictionary 您可以使用ResourceDictionary

Put all your generic templates and styles in a ResourceDictionary , then have both your UserControls import that ResourceDictionary 将所有通用模板和样式放在ResourceDictionary ,然后让UserControls导入ResourceDictionary

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

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

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