2

I have a scenario, where a treeview changes its data template and data binding definition dynamically. I created a treeview in XAML like this:

<TreeView x:Name="BimTreeView">

</TreeView>

I didn't defined the data template and binding definition in XAML. Because the data template and binding definition must have to be changed by user's preference.

I tried the following C# code that I found here to create the data template definition dynamically. However, looking at the following code, I couldn't figure out how to change data binding definition via C# code.

private DataTemplate GetHeaderTemplate()
{
    //create the data template
    DataTemplate dataTemplate = new DataTemplate();

    //create stack pane;
    FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.Name = "parentStackpanel";
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    // Create check box
    FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
    checkBox.Name = "chk";
    checkBox.SetValue(CheckBox.NameProperty, "chk");
    checkBox.SetValue(CheckBox.TagProperty, new Binding());
    checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2));
    checkBox.SetValue(CheckBox.TagProperty, new Binding() { Path = new PropertyPath("Name") });
    stackPanel.AppendChild(checkBox);

    // Create Image 
    FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
    image.SetValue(Image.MarginProperty, new Thickness(2));
    image.SetBinding(Image.SourceProperty, new Binding() { Path = new PropertyPath("ImageUrl") });
    stackPanel.AppendChild(image);

    // create text
    FrameworkElementFactory label = new FrameworkElementFactory(typeof(TextBlock));
    label.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
    label.SetValue(TextBlock.ToolTipProperty, new Binding());

    stackPanel.AppendChild(label);


    //set the visual tree of the data template
    dataTemplate.VisualTree = stackPanel;

    return dataTemplate;

}

I would really appreciate if someone could explain how can I change the data template and bind treeview hierarchical data in code behind C#.

Thank you!!

5
  • 1
    what do you expect? This function should return a datatemplate instance that can be used on your TreeViewItem . You can just modify that instance. Commented May 30, 2018 at 7:19
  • I need the data template, along with the ability to change the data binding definition. For instance, in the current function, image source property is binded with 'ImageURL'. But I am not able to figure out where does the 'ImageURL' value comes from. Commented May 30, 2018 at 7:23
  • I think you have chosen a wrong way. Why do you need thing like this? Commented May 30, 2018 at 7:47
  • Because the data template and binding definition must have to be changed by user's preference. Can you share more light on this? Commented May 30, 2018 at 7:48
  • 2
    The datatemplate is returned by this function thus you already have it.In most cases you make the binding instance dynamic, not something trying to "rebinding" it. You can clear your binding by BindingOperations.ClearBinding and access your label or image using FindName. Howerver I suggest you making a ViewModel instead. Commented May 30, 2018 at 7:51

1 Answer 1

1

Here's the remedy to the above code. The following code can now dynamically bind and can dynamically create datatemplate for the treeview in wpf.

public static void FillTree()
{
    BIMExplorerUserControl.Instance.BimTreeView.ItemTemplate = GetTemplate();

    BIMExplorerUserControl.Instance.BimTreeView.ItemsSource = ViewModel.Instance.DefaultExplorerView;
}

public static HierarchicalDataTemplate GetTemplate()
{
    //create the data template
    HierarchicalDataTemplate dataTemplate = new HierarchicalDataTemplate();

    //create stack pane;
    FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.Name = "parentStackpanel";
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    ////// Create check box
    FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
    checkBox.Name = "chk";
    checkBox.SetValue(CheckBox.NameProperty, "chk");
    checkBox.SetValue(CheckBox.TagProperty, new Binding());
    checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2));
    checkBox.SetValue(CheckBox.TagProperty, new Binding() { Path = new PropertyPath("Name") });
    stackPanel.AppendChild(checkBox);


    // create text
    FrameworkElementFactory label = new FrameworkElementFactory(typeof(TextBlock));
    label.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
    label.SetValue(TextBlock.MarginProperty, new Thickness(2));
    label.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
    label.SetValue(TextBlock.ToolTipProperty, new Binding());

    stackPanel.AppendChild(label);


    dataTemplate.ItemsSource = new Binding("Elements");

    //set the visual tree of the data template
    dataTemplate.VisualTree = stackPanel;

    return dataTemplate;

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.