首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在XAML中传递XAML对象

在XAML中传递XAML对象
EN

Stack Overflow用户
提问于 2013-10-16 11:36:59
回答 1查看 84关注 0票数 0

背景:

我在XAML和类中有一个完全代表这个标记的配置文件。

我的xaml-文件如下所示:

代码语言:javascript
复制
<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>

我首先尝试在一个额外的名称空间中实现第二个ElementListElement类,这样我就可以使用不同的命名空间在配置的ElementGoup属性中定义分组元素。但是这种方法对我来说似乎是不雅的和多余的。

我读过关于x:Reference的文章,我认为实现DependencyProperty是可能的,但我不确定在这里需要什么是最好的方法。因为这只是一个配置,并且在应用程序启动时只被解析一次。所以没有真正的需要绑定,还是我错了?

我怎么知道我在找什么?是否有比我所能找到的更好的方法来引用xaml标记中的对象?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-17 09:10:07

我为了树错过了森林..。经过一番努力,我可以自己解决问题。

解决方案在于我前面提到的两种方法的结合。

我将属性<ElementGroup.Elements>实现为DependencyProperty,并使用DataBinding引用<RootConfiguration.Elements>中定义的元素。

代码语言:javascript
复制
#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标记中的元素,如下所示:

代码语言:javascript
复制
<RootConfiguration.ElementGroups>
    <ElementGroupList>
        <ElementsGroup Name="All Elements" Elements="{x:Reference completeElementDefinition}" />
    <!-- [...] -->
    </ElementGroupList>   
</RootConfiguration.ElementGroups>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19402369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档