在WPF树视图节点设置父字体为粗体使用code背后 [英] Setting font of parent in nodes in WPF Tree View to Bold using code behind

查看:460
本文介绍了在WPF树视图节点设置父字体为粗体使用code背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个树视图,并添加父和子节点,它全部采用落后code,现在我在哪里卡住是设置父节点的字体粗体,同时保持孩子的字体,因为它是,下面是code,我现在所拥有的。

I have made a tree view and added parent and child nodes to it all using code behind, now where I am stuck is to set the font of parent nodes to BOLD while keeping the font of child as it is, below is code that i have right now.

 List<ParentTreeViewNode> ParentTreeViewNodeList = new List<ParentTreeViewNode>();

                HierarchicalDataTemplate treeViewTemplate = new HierarchicalDataTemplate(typeof(Child));
                treeViewTemplate.DataType = "{x:Type local:Child}";
                treeViewTemplate.ItemsSource = new Binding("Children");

                FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));
                tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));
                tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow);
                treeViewTemplate.VisualTree = tb;

                DataTemplate parentTemplate = new DataTemplate(typeof(ParentTreeViewNode));
                parentTemplate.DataType = "{x:Type local:ParentTreeViewNode}";
                //parentTemplate.ItemsSource = new Binding("Children");

                FrameworkElementFactory tbp = new FrameworkElementFactory(typeof(TextBlock));
                tbp.SetBinding(TextBlock.TextProperty, new Binding("Name"));
                tbp.SetValue(TextBlock.ForegroundProperty, Brushes.Green);
                treeViewTemplate.VisualTree = tbp;


                // ParentTreeViewNode1.Children = Childlist1;
                ;
                ParentTreeViewNodeList.Add(new ParentTreeViewNode("Paren1"));
                ParentTreeViewNodeList.Add(new ParentTreeViewNode("Paren2"));
                ParentTreeViewNodeList.Add(new ParentTreeViewNode("Paren3"));

                //arrayTreeView.ItemTemplate = treeViewTemplate;
                arrayTreeView.Resources.Add(1,treeViewTemplate);
                arrayTreeView.Resources.Add(2,treeViewTemplate);
                arrayTreeView.ItemsSource = ParentTreeViewNodeList;  

这是我花了帮助,从链接: <一href="http://zamjad.word$p$pss.com/2009/12/06/using-hierarchical-data-template-with-c-$c$c/#comment-446" rel="nofollow">http://zamjad.word$p$pss.com/2009/12/06/using-hierarchical-data-template-with-c-$c$c/#comment-446

this is the link where i took help from: http://zamjad.wordpress.com/2009/12/06/using-hierarchical-data-template-with-c-code/#comment-446

感谢

推荐答案

试试这个

  <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <Grid>
                        --------------
                        --------------
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="true">
                            <Setter TargetName="PART_Header"
                                    Property=" TextElement.FontWeight"
                                    Value="Bold"/>
                        </Trigger>                           
                    </ControlTemplate.Triggers>
                </ControlTemplate>

复制默认模板并添加此trigger.I我不知道如何做到这一点在code.You可以查看这篇文章得到一些概述

Copy the default template and add this trigger.I am not sure how to do this in code.You can check this article to get some overview

HTTP://www.$c$cproject.com/KB/按钮/ ButtonControl.aspx

这篇关于在WPF树视图节点设置父字体为粗体使用code背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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