XAML继承(不包含代码) [英] XAML Inheritance (not code-behind)
问题描述
如何在 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.xaml
和File2.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.xaml
和File2.xaml
中唯一与不同的是:
-
Button
(请参见代码中的注释) -
XamDataGrid
的属性,主要是事件(它们在每种情况下的作用).
ToolBar
中的Button
s in theToolBar
(see comment in the code)- 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屋!