[求助]wpf中TreeView的显示问题

h82258652 h82258652 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
想做一个基于WPF的目录树
具体的效果是这样的

每个TreeViewItem显示文件夹
分别显示图标、文件夹名称和当前文件夹的子文件夹个数

现在问题就是第三项显示“当前文件夹的子文件夹个数”这一项总是为0。(调试有数值,但界面显示0)
前台代码
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView>
<loc:MyTreeViewItem x:Name="MyFiles">
<loc:MyTreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="/WpfApplication2;component/Images/Folder.png" />
<TextBlock Text="我的文件"></TextBlock>
<TextBlock Text="{Binding Count}" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</loc:MyTreeViewItem.Header>
<loc:MyTreeViewItem>
<loc:MyTreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="/WpfApplication2;component/Images/Folder.png" />
<TextBlock Text="我的文件的文件"></TextBlock>
<TextBlock Text="{Binding Count}" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</loc:MyTreeViewItem.Header>
</loc:MyTreeViewItem>
</loc:MyTreeViewItem>
</TreeView>
</Grid>
</Window>


后台TreeViewItem.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace WpfApplication2
{
class MyTreeViewItem:TreeViewItem
{
public int Count
{
get
{
return this.Items.Count;
}
}

public MyTreeViewItem()
{
this.DataContext = this;
}
}
}


由于Count属性是只读的,因此INotifyPropertyChanged接口也用不上了。
求cdsn的大牛指导。

另外我将TreeViewItem封装成UserControl再添加到的话,在TreeView中就没法选中封装好的UserControl(也就是没法选中然后背景变蓝),求大牛指导指导。
...全文
评论 6
收藏 已收藏
分享
返回顶部
h82258652
C# 111,129 成员 642,538 内容