110,567
社区成员
发帖
与我相关
我的任务
分享
<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>
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;
}
}
}
<Style TargetType="MyTreeViewItem">
<Setter Property="Template">
</Setter>
</Style>
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
上面的是基本定义,然后你在添加子项时调用OnPropertyChanged("Count")方法即可,如果你可以确定是哪个方法添加的子项,并且那个方法能被override,那么直接在该方法内调用OnPropertyChanged("Count")方法即可,那样就不需要设置为public了。