WPF Binding when setting DataTemplate Programically

Posted by Daniel on Stack Overflow See other posts from Stack Overflow or by Daniel
Published on 2010-03-28T20:45:34Z Indexed on 2010/03/28 20:53 UTC
Read the original article Hit count: 284

Hello,

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">
</layoutToolkit:Accordion>

and DateTemplate 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
                }
            }
  }

I really need help with this issue. Thanks for any support.

Daniel

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding