Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Cannot looping through wpf treeview parent and its child node ...I get Parent node value but its child return null value.

Below is my WPF treeview and #code how it is binding . I am using DataContext to bind data , which is working fine .

I want to hide / show the ImgLogin image to dynamically from code behind.


HTML
<treeview margin="368,61,0,0" itemssource="{Binding}" x:name="tvGroupUsers" horizontalalignment="Left" verticalalignment="Top" width="245" height="375" rendertransformorigin="-4.929,1.701" grid.column="1" xmlns:x="#unknown">
          <treeview.resources>
              <datatemplate x:key="childrenDataTemplate">
                  <stackpanel orientation="Horizontal">
                      <border name="CornersMask" removed="SkyBlue" cornerradius="50" height="30" width="30" horizontalalignment="Left" margin="-25,5,5,5" verticalalignment="Top" />
                      <Image  Name="image1" Source="{Binding DisplayedImage}" Stretch="Fill" UseLayoutRounding="True" Height="30" Width="30"   HorizontalAlignment="Left" Margin="-34,4,0,0"  VerticalAlignment="Top">
                          <Image.OpacityMask>
                              <visualbrush visual="{Binding ElementName=CornersMask}" />
                          </Image.OpacityMask>
                          <Image.Effect>
                              <dropshadoweffect />
                          </Image.Effect>
                      </Image>
                      <Label Content="{Binding Path=Name}"></Label>
                      <Image  Name="ImgAlert" Visibility="Visible" Height="16" Width="16" Tag="{Binding Path=UserID}" Source="{Binding ImgAlertMsg}"></Image>
                      <Image  Name="ImgLogin"  Visibility="Visible"  Height="15" Width="16" Tag="{Binding Path=UserID}" Source="{Binding ImgLogin}"></Image>
                      <Label Content="{Binding Path=UserID}" Visibility="Hidden"/>
                  </stackpanel>
              </datatemplate>
          </treeview.resources>
          <treeview.itemtemplate>
              <hierarchicaldatatemplate itemssource="{Binding Path=ClientsList}" itemtemplate="{StaticResource ResourceKey=childrenDataTemplate}">
                  <textblock text="{Binding Path=ChatGroup}" fontweight="Bold" />
              </hierarchicaldatatemplate>
          </treeview.itemtemplate>
      </treeview>


Below is my Code behind code ..

C#
lstGroups = proxy.GetExistingGroups();
                    if (lstGroups.Count > 0)
                        {
                            for (int i = 0; i < lstGroups.Count; i++)
                            {
                                SVC.Groups objGroups = new SVC.Groups();
                                objGroups.GroupID = lstGroups[i].GroupID;
                                objGroups.ChatGroup = objUtilities.ConvertToCamelCase(lstGroups[i].ChatGroup);

                                if (objGroups.GroupID == 0)
                                {
                                    lstClient = proxy.GetUsers(this.localClient.UserID.ToString());
                                    finallstClient = new List<SVC.Client>();
                                    for (int k = 0; k < lstClient.Count; k++)
                                    {
                                        objUtilities = new Utilities();
                                        SVC.Client objClient = new SVC.Client();
                                        objClient.DisplayedImage = "";
                                        objClient.FirstName = lstClient[k].FirstName;
                                        objClient.LastName = lstClient[k].LastName;
                                        objClient.UserID = lstClient[k].UserID;
                                        objClient.PersonalMail = lstClient[k].PersonalMail;
                                        objClient.Name = lstClient[k].FirstName + " " + lstClient[k].LastName;
                                        objClient.Picture = lstClient[k].Picture;
                                        objClient.ImgAlertMsg = ImgAlertMsg;
                                        objClient.ImgLogin = ImgLogin;

                                        if (lstClient[k].Picture != null)
                                        {
                                            objClient.DisplayedImage = CreateUserImages(objClient);
                                        }
                                        else
                                        {
                                            objClient.DisplayedImage = defaultPath;
                                        }
                                        finallstClient.Add(objClient);
                                        AllClients.Add(lstClient[k].UserID.ToString(), objClient);
                                    }
                                    objGroups.ClientsList = finallstClient;
                                }
                                else
                                {
                                    lstClient = proxy.GetGroupUsers(objGroups.GroupID.ToString(), this.localClient.UserID.ToString());
                                    if (lstClient.Count > 0)
                                    {
                                        finallstClient = new List<SVC.Client>();
                                        for (int j = 0; j < lstClient.Count; j++)
                                        {
                                            objUtilities = new Utilities();
                                            SVC.Client objClient = new SVC.Client();
                                            objClient.DisplayedImage = "";
                                            objClient.FirstName = lstClient[j].FirstName;
                                            objClient.LastName = lstClient[j].LastName;
                                            objClient.UserID = lstClient[j].UserID;
                                            objClient.PersonalMail = lstClient[j].PersonalMail;
                                            objClient.Name = lstClient[j].FirstName + " " + lstClient[j].LastName;
                                            objClient.Picture = lstClient[j].Picture;
                                            objClient.ImgAlertMsg = ImgAlertMsg;
                                            objClient.ImgLogin = ImgLogin;

                                            if (lstClient[j].Picture != null)
                                            {
                                                objClient.DisplayedImage = CreateUserImages(objClient);
                                            }
                                            else
                                            {
                                                objClient.DisplayedImage = defaultPath;
                                            }
                                            finallstClient.Add(objClient);
                                        }
                                        objGroups.ClientsList = finallstClient;
                                    }
                                }
                                finallstGroups.Add(objGroups);
                            }

                            tvGroupUsers.DataContext = finallstGroups;

Its urgent Please reply asap...
Thanks in Advance..

What I have tried:

C#
foreach (object parent in items.Items)
            {
                TreeViewItem treeItem = this.tvGroupUsers.ItemContainerGenerator.ContainerFromItem(parent) as TreeViewItem;
                ItemsControl childControl = ItemsControl.ItemsControlFromItemContainer(treeItem);

                foreach (object child in treeItem.Items)
                {
                    TreeViewItem childtreeview = this.tvGroupUsers.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
                }               
            }


childtreeview return null
Posted
Updated 22-Mar-16 21:38pm
v2

1 solution

Use this Helper method in code behind

C#
T GetVisualChild<t>(DependencyObject parent, string name) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<t>(v, name);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

</t></t>


You just Need is
for eg. Declare Your Image First in your code behind as

C#
Image ImgAlert=new Image();

in your constructor or Loaded method

then on your event You can call this method by
C#
ImgAlert=GetVisualChild<treeview>(this, "ImgAlert");
</treeview>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900