c# – 向TreeViewItem添加图标
发布时间:2020-12-16 00:06:07 所属栏目:百科 来源:网络整理
导读:我被要求更新一个在类对象中动态构建的 WPF应用程序中的TreeVeiw.正如您将看到treeveiw没有任何约束.以下不是我的代码!! TreeView Grid.Row =“0”Horizo??ntalAlignment =“Stretch”Name =“tvLocations”VerticalAlignment =“Stretch”SelectedItemChang
|
我被要求更新一个在类对象中动态构建的
WPF应用程序中的TreeVeiw.正如您将看到treeveiw没有任何约束.以下不是我的代码!!
< TreeView Grid.Row =“0”Horizo??ntalAlignment =“Stretch”Name =“tvLocations”VerticalAlignment =“Stretch”SelectedItemChanged =“tvLocations_SelectedItemChanged”/> private void BuildTreeVeiw(Location locationList)
{
this.Title = _selectedLoc.Name + " - Locations";
tvLocations.Items.Clear();
TreeViewItem tvitem;
tvitem = new TreeViewItem() { Header = locationList.Name,Uid = locationList.Id.ToString() };
if (locationList.Printers != null)
{
TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
tvprnitem.FontWeight = FontWeights.Regular;
foreach (Printer sprinters in locationList.Printers)
{
TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name,Uid = sprinters.Id.ToString() };
TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ",sprinters.UNC) };
psubitem.Items.Add(psubitem1);
tvprnitem.Items.Add(psubitem);
}
tvitem.Items.Add(tvprnitem);
}
foreach (Location loc in locationList.Children)
{
AddChildren(loc,ref tvitem);
}
tvLocations.Items.Add(tvitem);
}
private void AddChildren(Location child,ref TreeViewItem tvi)
{
TreeViewItem tvitem;
tvitem = new TreeViewItem() { Header = child.Name,Uid = child.Id.ToString() };
if (child.Name == _currentLocation.Name)
{
tvitem.FontWeight = FontWeights.Bold;
}
if (child.Printers != null)
{
TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
tvprnitem.FontWeight = FontWeights.Regular;
foreach (Printer sprinters in child.Printers)
{
TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name,sprinters.UNC) };
psubitem.Items.Add(psubitem1);
tvprnitem.Items.Add(psubitem);
}
tvitem.Items.Add(tvprnitem);
}
if (child.Children != null)
{
foreach (Location loc in child.Children)
{
AddChildren(loc,ref tvitem);
}
}
tvi.Items.Add(tvitem);
}
这正确地构建了树,我被要求做的就是向TreeViewItem添加一个图标.根据位置或该位置内的打印机,图标将有所不同. 我看不到如何向TreeViewItems添加图标,任何人都可以指向正确的方向吗? 解决方法
我通过改变这一行来解决这个问题
tvitem = new TreeViewItem() { Header = child.Name,Uid = child.Id.ToString() };
至 tvitem = GetTreeView(child.Id.ToString(),child.Name,"location.png"); 添加此功能 private TreeViewItem GetTreeView(string uid,string text,string imagePath)
{
TreeViewItem item = new TreeViewItem();
item.Uid = uid;
item.IsExpanded = false;
// create stack panel
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
// create Image
Image image = new Image();
image.Source = new BitmapImage
(new Uri("pack://application:,/Images/" + imagePath));
image.Width = 16;
image.Height = 16;
// Label
Label lbl = new Label();
lbl.Content = text;
// Add into stack
stack.Children.Add(image);
stack.Children.Add(lbl);
// assign stack to header
item.Header = stack;
return item;
}
工作得很完美. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- BB FlashBack Pro v4.0.0.2375 incl. crack/serial by ADMI
- c# – 计算特殊的UTF-8字符
- js正则表达式学习笔记
- ruby-on-rails – 如何在Rails 4应用程序中禁用IP Spoofing
- 如何在C#中将json转换为扁平结构
- SQLite Where 子句(http://www.w3cschool.cc/sqlite/sqlite
- Unity 3d SQLite Database connection
- winforms – C# – 通过特殊的方式将水印添加到照片中
- postgresql – 向中国插入内容时的延迟问题
- c# – 检查包含数字的字符串值的范围
