背景:
我在XAML和类中有一个完全代表这个标记的配置文件。
我的xaml-文件如下所示:
<RootConfiguration
xmlns="clr-namespace:Configuration;assembly=Configuration"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<RootConfiguration.Elements>
<ElementList x:Name="completeElementDefinition">
<Element Type="X" Show="True" Refresh="30">
<Element.Name>A1</Element.Name>
</Element>
<Element Type="Y" Show="True" Refresh="30">
<Element.Name>B1</Element.Name>
</Element>
</ElementList>
</RootConfiguration.Elements>
<RootConfiguration.ElementGroups>
<ElementGroupList>
<ElementGroup Name="All Elements">
<ElementGroup.Elements>
<!-- How to reference the ElementList "completeElementDefinition" defined above -->
</ElementGroup.Elements>
</ElementGroup>
<ElementGroup Name="Type X Elements">
<ElementGroup.Elements>
<ElementList>
<!-- How to reference single Elements from the list "completeElementDefinition" -->
</ElementList>
</ElementGroup.Elements>
</ElementGroup>
<ElementGroup Name="Type Y Elements">
<ElementGroup.Elements>
<ElementList>
<!-- How to reference single Elements from the list "completeElementDefinition" -->
</ElementList>
</ElementGroup.Elements>
</ElementGroup>
</ElementGroupList>
</RootConfiguration.ElementGroups>
</RootConfiguration>我首先尝试在一个额外的名称空间中实现第二个ElementList和Element类,这样我就可以使用不同的命名空间在配置的ElementGoup属性中定义分组元素。但是这种方法对我来说似乎是不雅的和多余的。
我读过关于x:Reference的文章,我认为实现DependencyProperty是可能的,但我不确定在这里需要什么是最好的方法。因为这只是一个配置,并且在应用程序启动时只被解析一次。所以没有真正的需要绑定,还是我错了?
我怎么知道我在找什么?是否有比我所能找到的更好的方法来引用xaml标记中的对象?
发布于 2013-10-17 09:10:07
我为了树错过了森林..。经过一番努力,我可以自己解决问题。
解决方案在于我前面提到的两种方法的结合。
我将属性<ElementGroup.Elements>实现为DependencyProperty,并使用DataBinding引用<RootConfiguration.Elements>中定义的元素。
#region Dependancy Property
// Dependency Property
public static readonly DependencyProperty ElementsProperty =
DependencyProperty.Register("Elements", typeof(ElementList),
typeof(ElementGroup), new PropertyMetadata(null));
// .NET Property wrapper
public ElementList Elements {
get { return (ElementList )GetValue(ElementsProperty ); }
set { SetValue(ElementsProperty , value); }
}
#endregion现在,我可以查看XAML标记中的元素,如下所示:
<RootConfiguration.ElementGroups>
<ElementGroupList>
<ElementsGroup Name="All Elements" Elements="{x:Reference completeElementDefinition}" />
<!-- [...] -->
</ElementGroupList>
</RootConfiguration.ElementGroups>https://stackoverflow.com/questions/19402369
复制相似问题