1
votes

I have my little designer tool (my program).

On the left side I have TreeView and on the right site I have Accordion.

When I select a node I want to dynamically build Accordion Items based on Properties from DataContext of selected node.

Selecting nodes works fine, and when I use this sample code for testing it works also.

XAML code:

<layoutToolkit:Accordion x:Name="accPanel"
                         SelectionMode="ZeroOrMore"
                         SelectionSequence="Simultaneous">
  <layoutToolkit:AccordionItem Header="Controller Info">
    <StackPanel Orientation="Horizontal" DataContext="{Binding}">
      <TextBlock Text="Content:" />
      <TextBlock Text="{Binding Path=Name}" />
    </StackPanel>
  </layoutToolkit:AccordionItem>
</layoutToolkit:Accordion>

C# code:

private void treeSceneNode_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
   if (e.NewValue != e.OldValue)
   {
      if (e.NewValue is SceneNode)
      {
         accPanel.DataContext = e.NewValue; //e.NewValue is a class that contains Name property
      }
   }
 }

But the problem occurs when I'm trying to achive this using DateTemplate and dynamically build AccordingItem, the Binding is not working:

<layoutToolkit:Accordion x:Name="accPanel"
                         SelectionMode="ZeroOrMore"
                         SelectionSequence="Simultaneous" />

and DataTemplate in my ResourceDictionary

<DataTemplate x:Key="dtSceneNodeContent">
   <StackPanel Orientation="Horizontal" DataContext="{Binding}">
      <TextBlock Text="Content:" />
      <TextBlock Text="{Binding Path=Name}" />
   </StackPanel>
</DataTemplate>

and C# code:

private void treeSceneNode_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
  if (e.NewValue != e.OldValue)
  {
    ResourceDictionary rd = new ResourceDictionary();
    rd.Source = new Uri("/SilverGL.GUI;component/SilverGLDesignerResourceDictionary.xaml", UriKind.RelativeOrAbsolute);

    if (e.NewValue is SceneNode)
    {
      accPanel.DataContext = e.NewValue;

      AccordionItem accController = new AccordionItem();
      accController.Header = "Controller Info";
      accController.ContentTemplate = rd["dtSceneNodeContent"] as DataTemplate;

      accPanel.Items.Add(accController);
    }
    else
    {
      // Other type of node
    }
  }
}
1
Could you expand on "not working" -- exception, incorrect result, nothing happens? Do you see any binding errors in the Output window?itowlson
Hello itowlson, I don't have any exception or error in the Output. Simply, first "correct" "static" example shows me "Content: Scene Root Node", where "Scene Root Node" comes from Name Property. Second "wrong", "dynamic" example shows me only "Content: ". And I can't figure out where is the problem. Yes, I've checked Output window and nothing is there.Daniel Skowroński

1 Answers

0
votes

Are you missing this?

accController.Content = e.NewValue;

Also, I don't think you need to use DataContext="{Binding}"; the DataContext will inherit anyway.