Search code examples
c#.netxamlmarkup

Pass XAML object within XAML


Background:

I have a configuration file completely in XAML and classes which represents this markup.

My xaml-file looks like this:

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

I tried first to implement a second ElementList and Element class in an extra namespace, so that i could define the grouped elements in the ElementGoup-Property of the configuration by using the different namespace. But this approach seems to be inelegant and redundant to me.

I've read about x:Reference and I think it would be possible to implement a DependencyProperty but I'm not sure whats the best approach for what i'm needing here. Since this is just a configuration and is parsed only once at the application start-up. So there is no real need for Bindings or am I wrong?

How can I realize what I'm looking for? Is there a better way to reference objects in my xaml-markup than I was able to find?


Solution

  • I missed the forest for the trees... Due some reseach effort I could solve my problem by myself.

    The solution lied in the combination of the two approaches i mentioned above.

    I implemented the Property <ElementGroup.Elements> as a DependencyProperty and used a DataBinding to reference the Elements defined in <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
    

    Now I can reffer to the Elements in my XAML-Markup like shown here:

    <RootConfiguration.ElementGroups>
        <ElementGroupList>
            <ElementsGroup Name="All Elements" Elements="{x:Reference completeElementDefinition}" />
        <!-- [...] -->
        </ElementGroupList>   
    </RootConfiguration.ElementGroups>