在WPF中构建标签云

时间:2011-05-01 12:22:58

标签: wpf xaml tag-cloud

我正在尝试基于an existing implementation [Download Source]在WPF中构建标记云。我完全不了解实现,我的问题是,我没有将FontSize绑定到集合中的项目数,而是将其绑定到类中包含的其他值。所以在这里,

FontSize="{Binding Path=ItemCount, Converter={StaticResource CountToFontSizeConverter}}"

我想将FontSize绑定到其他地方。我怎么做? ItemCount属于哪里?

由于

2 个答案:

答案 0 :(得分:2)

ItemCount属于从该代码生成的集合视图中的group

e.g。如果我有一个清单

  

A A B B B C

我将他们归为一组:

  

A组:ItemCount = 2
  B组:ItemCount = 3
  C组:ItemCount = 1

Tag-Cloud的重点是绑定到该属性,因为您想要可视化某个标记的使用频率。


要回复您的评论,简单的设置应该是这样的:

<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>

我假设您的数据对象类公开了属性NameCount,以确保大小随着计数的增加而变化,数据对象类需要实现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));
        }
    }
}

答案 1 :(得分:0)

ItemCount是要更改FontSize的WPF对象的DataContext属性中包含的任何实例的属性。在层次结构树中,从FrameworkElement开始的所有内容都继承了“DataContext”属性。

使用"snoop",您可以在运行时查看WPF应用程序的UI树,例如找出在任何给定时间在DataContext中存在的对象。