Click here to Skip to main content
16,008,719 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: "Embed" an external video clip Pin
McCombi23-Aug-11 22:49
McCombi23-Aug-11 22:49 
GeneralRe: "Embed" an external video clip Pin
Navin Pandit23-Aug-11 23:50
Navin Pandit23-Aug-11 23:50 
AnswerRe: "Embed" an external video clip Pin
McCombi24-Aug-11 3:07
McCombi24-Aug-11 3:07 
QuestionWPF help- how to access a textbox value from a different window Pin
steersteer22-Aug-11 11:17
steersteer22-Aug-11 11:17 
AnswerRe: WPF help- how to access a textbox value from a different window Pin
Mycroft Holmes22-Aug-11 13:09
professionalMycroft Holmes22-Aug-11 13:09 
GeneralRe: WPF help- how to access a textbox value from a different window Pin
steersteer22-Aug-11 13:47
steersteer22-Aug-11 13:47 
GeneralRe: WPF help- how to access a textbox value from a different window Pin
Mycroft Holmes22-Aug-11 14:04
professionalMycroft Holmes22-Aug-11 14:04 
QuestionLosing expanded nodes in TreeView after ItemsSource has updated Pin
Mc_Topaz21-Aug-11 21:58
Mc_Topaz21-Aug-11 21:58 
I have a TreeView which I update it's ItemsSource property during runtime. The ItemsSource is assigned with multiple levels of data. During runtime I expand and collapse levels in the TreeView, but when I update the TreeView the expanded nodes are collapsed. Since I basically update the TreeView every time I select a node, to display it's child nodes, it gets very annoying to keep expanding the same nodes every time to watch the new level.

I understand why this is a issue for me because I set the ItemsSource property to null and than sets it with the proper data to update the TreeView's visualization. So of course all node are collapsed...

Here is an example of the code I use (It can be copied and pasted and tested right away):
XAML
<Window x:Class="Tree.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Tree"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="tvTree">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SubNodes}" DataType="{x:Type local:Node}">
                    <Grid>
                        <TextBlock Text="{Binding Header}" />
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <Button Content="Button" Height="50" HorizontalAlignment="Left" Margin="456,263,0,0" Name="button1" VerticalAlignment="Top" Width="47" Click="button1_Click" />
    </Grid>
</Window>


The C# behind it:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tree
{
    public partial class MainWindow : Window
    {
        Node top;
        Node root;

        public MainWindow()
        {
            InitializeComponent();
                        
            root = new Node();
            root.Header = "Root";

            root.SubNodes.Add(new Node("B1"));

            top = new Node();
            top.SubNodes.Add(root);

            Update();
        }

        public void Update()
        {
            tvTree.ItemsSource = null;
            tvTree.ItemsSource = top.SubNodes;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            root.SubNodes.Add(new Node("B2"));
            Update();
        }
    }

    public class Node
    {
        List<Node> subNodes = new List<Node>();
        string header;

        public Node()
        {

        }

        public Node(string header)
        {
            this.header = header;
        }

        public string Header
        {
            get { return header; }
            set { header = value; }
        }

        public List<Node> SubNodes
        {
            get { return subNodes; }
            set { subNodes = value; }
        }
    }
}


I use the button to add new data into the TreeView. Notice the Update() method which updates the TreeView's ItemsSource.

Can I in some way expand the nodes in the TreeView after I have changed the ItemsSource? I can keep track of which nodes are expanded or not (I left out that code in the example). I know about the .IsExpanded property in the TreeViewItem class, but in my case I don't have TreeViewItem in my ItemsSource so therefore I don't have any .IsExpanded property to use.

In some way I feel this approach maybe not be the best. Especially how I update the TreeView item. If there are any other way please tell me and tell me how.
AnswerRe: Losing expanded nodes in TreeView after ItemsSource has updated Pin
Mycroft Holmes21-Aug-11 22:42
professionalMycroft Holmes21-Aug-11 22:42 
GeneralRe: Losing expanded nodes in TreeView after ItemsSource has updated Pin
Mc_Topaz22-Aug-11 1:48
Mc_Topaz22-Aug-11 1:48 
GeneralRe: Losing expanded nodes in TreeView after ItemsSource has updated Pin
Mycroft Holmes22-Aug-11 12:20
professionalMycroft Holmes22-Aug-11 12:20 
Questionapple mac laptop Pin
arkiboys20-Aug-11 0:13
arkiboys20-Aug-11 0:13 
AnswerRe: apple mac laptop Pin
Mehdi Gholam20-Aug-11 0:39
Mehdi Gholam20-Aug-11 0:39 
GeneralRe: apple mac laptop Pin
arkiboys20-Aug-11 5:50
arkiboys20-Aug-11 5:50 
AnswerRe: apple mac laptop Pin
BubingaMan5-Sep-11 4:33
BubingaMan5-Sep-11 4:33 
QuestionSilverlight with DevExpress Grid Control Pin
brijmohansingh1018-Aug-11 20:07
brijmohansingh1018-Aug-11 20:07 
AnswerRe: Silverlight with DevExpress Grid Control Pin
Abhinav S20-Aug-11 1:45
Abhinav S20-Aug-11 1:45 
QuestionNeed some Simple Code Pin
peterdrozd15-Aug-11 6:49
peterdrozd15-Aug-11 6:49 
AnswerRe: Need some Simple Code Pin
Kevin Marois15-Aug-11 10:25
professionalKevin Marois15-Aug-11 10:25 
AnswerRe: Need some Simple Code Pin
SledgeHammer0115-Aug-11 10:41
SledgeHammer0115-Aug-11 10:41 
GeneralRe: Need some Simple Code Pin
Kevin Marois15-Aug-11 11:52
professionalKevin Marois15-Aug-11 11:52 
GeneralRe: Need some Simple Code Pin
Mycroft Holmes15-Aug-11 13:01
professionalMycroft Holmes15-Aug-11 13:01 
GeneralRe: Need some Simple Code Pin
SledgeHammer0115-Aug-11 13:39
SledgeHammer0115-Aug-11 13:39 
GeneralRe: Need some Simple Code Pin
Mycroft Holmes15-Aug-11 21:05
professionalMycroft Holmes15-Aug-11 21:05 
GeneralRe: Need some Simple Code Pin
SledgeHammer0116-Aug-11 4:34
SledgeHammer0116-Aug-11 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.