无法循环遍历wpf treeview parent及其子节点...我获取父节点值但其子节点返回null值。 [英] Cannot looping through wpf treeview parent and its child node ...i get parent node value but its child return null value.

查看:59
本文介绍了无法循环遍历wpf treeview parent及其子节点...我获取父节点值但其子节点返回null值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法循环遍历wpf treeview parent及其子节点...我得到父节点值但其子节点返回空值。



下面是我的WPF树视图和#code如何绑定。我使用DataContext绑定数据,这是正常的。



我想隐藏/显示ImgLogin图像,以便从代码后面动态显示。





 <   treeview     margin   =  368 ,61,0,0    itemssource   =  {Binding}    x:名称  =  tvGroupUsers    horizo​​ntalalignment   =     verticalalignment   = 顶部    width   =  245    height   =  375    rendertransformorigin   =  -4.929,1.701    grid.column   =  1    xmlns: x   = #unknown >  
< treeview.resources >
< datatemplate x:key = childrenDataTemplate >
< stackpanel orientation = 水平 >
< border 名称 = CornersMask 已删除 = SkyBlue cornerradius = 50 height = 30 width = 30 horizo​​ntalalignment = margin = - 25,5,5,5 verticalalignment = Top / >
< 图像 名称 = image1 Source = {Binding DisplayedImage} 拉伸 = 填充 UseLayoutRounding = True 高度 = 30 宽度 = 30 Horizo​​ntalAlignment = 保证金 = -34,4,0,0 VerticalAlignment = Top >
< Image.OpacityMask >
< visualbrush visual = {Binding ElementName = CornersMask} / >
< /Image.OpacityMask>
< Image.Effect >
< dropshadoweffect / >
< / Image.Effect >
< / Image >
< 标签 内容 = {Binding Path = Name} > < / Label >
< 图像 名称 = ImgAlert 可见性 = 可见 高度 = 16 宽度 = 16 标签 = {Binding Path = UserID} 来源 = {Binding ImgAlertMsg} > < / Image >
< 图像 名称 = ImgLogin < span class =code-attribute> 可见性 = 可见 高度 = 15 < span class =code-attribute>宽度 = 16 标记 = {Binding Path = UserID} 来源 = {Binding ImgLogin} > < / Image >
< 标签 内容 = {Binding Path = UserID} 可见性 = 隐藏 / >
< / stackpanel >
< / datatemplate >
< / treeview.resources >
< treeview.itemtemplate >
< hierarchicaldatatemplate itemssource = {Binding Path = ClientsList} < span class =code-attribute> itemtemplate = {StaticResource ResourceK ey = childrenDataTemplate} >
< textblock text = {Binding Path = ChatGroup} fontweight = 粗体 / >
< / hierarchicaldatatemplate >
< / treeview.itemtemplate >
< / treeview >





以下是代码背后的代码..



 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 列表< 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] 。姓;
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(), .localClient.UserID.ToString());
if (lstClient.Count > 0
{
finallstClient = new 列表< 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] 。姓;
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;



紧急请尽快回复...

在此先感谢..



我尝试过:



< pre lang =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

解决方案

在代码背后使用此Helper方法



 T GetVisualChild< t>(DependencyObject parent, string  name)其中 T:Visual 
{
T child = 默认(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 >





您只需要

例如。在您的代码中首先声明您的图像



图像ImgAlert =  new 图像(); 



你的构造函数或加载方法



然后你的活动你可以这个叫方法by

 ImgAlert = GetVisualChild< treeview>( this   ImgAlert); 
< / treeview >


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.


<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 ..

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:

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

解决方案

Use this Helper method in code behind

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

Image ImgAlert=new Image();


in your constructor or Loaded method

then on your event You can call this method by

ImgAlert=GetVisualChild<treeview>(this, "ImgAlert");
</treeview>


这篇关于无法循环遍历wpf treeview parent及其子节点...我获取父节点值但其子节点返回null值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆