ItemCount
属于从该标签生成的集合视图内的 分组。
例如,如果我有一个列表:
A A B B B C
并对它们进行分组,结果如下:
Group A:ItemCount = 2
Group B:ItemCount = 3
Group C:ItemCount = 1
标签云的整个目的就是绑定到这个属性,因为你想可视化某个标签被使用的频率。
针对您的评论,基本设置应该像这样:
<ItemsControl ItemsSource="{Binding Data}">
<ItemsControl.Resources>
<vc:CountToFontSizeConverter x:Key="CountToFontSizeConverter"/>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Margin="2"
FontSize="{Binding Count, Converter={StaticResource CountToFontSizeConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我假设您的数据对象类公开了属性Name
和Count
,为确保随着计数的增加大小发生变化,该数据对象类需要实现INotifyPropertyChanged
,这就是全部内容。
public class Tag : INotifyPropertyChanged
{
private string _name = null;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private int _count = 0;
public int Count
{
get { return _count; }
set
{
if (_count != value)
{
_count = value;
OnPropertyChanged("Count");
}
}
}
//...
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
ItemCount是DataContext属性中包含的实例的属性,该属性属于您想要更改FontSize的WPF对象。在层次结构树中,从FrameworkElement
开始的所有内容都继承了“DataContext”属性。
使用{{link1:“snoop”}},您可以在运行时查看WPF应用程序的UI树,并确定任何给定时间内DataContext中存在哪些对象。
vc
是我在窗口中定义的自定义xmlns
,您可以像之前使用的实现一样定义转换器。顺便说一下,转换器名称甚至相同,但我在这里指定的转换器是我独立编写的实现,而不是之前使用的。 - H.B.