在WPF中构建标签云

发布于 2024-11-04 17:49:27 字数 481 浏览 0 评论 0原文

我正在尝试基于 现有实现 [下载源代码] 。我不完全理解实现,我的问题是,我不想将 FontSize 绑定到集合中的项目数,而是想将其绑定到类中包含的其他一些值。因此,在这一部分中,

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

我想将 FontSize 绑定到其他内容。我该怎么做? ItemCount 属于哪里?

谢谢

I'm trying to build a tag cloud in WPF based on an existing implementation [Download Source]. I didn't understand completely the implementation and my problem is, that instead of having the FontSize binded to the number of items in a collection, I want to bind it to some other values contained in a class. So in this part here,

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

I want to bind the FontSize to something else. How do I do that? Where does ItemCount belong to?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2024-11-11 17:49:28

ItemCount 属于 group 在从该标签生成的集合视图中。

例如,如果我有一个清单

AABBC

我将它们分组,得到:

A 组:ItemCount = 2
B 组:项目计数 = 3
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>

我假设您的数据对象类公开属性 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));
        }
    }
}

ItemCount belongs to the group inside the collection view that is generated from that Tag.

e.g. if i have a list

A A B B B C

And i group them i get:

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

The whole point of a Tag-Cloud is to bind to that very property because you want to visualize how often a certain tag is used.


To respond to your comments, the bare-bones setup should be something like this:

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

I assume that your data-object class exposes the properties Name and Count, to make sure that the size changes as count goes up that data-object class needs to implement INotifyPropertyChanged, that is about all there is to it.

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));
        }
    }
}
空城仅有旧梦在 2024-11-11 17:49:28

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

使用 "snoop",您可以在运行时查看 WPF 应用程序的 UI 树,例如找出哪些对象处于活动状态在任何给定时间都在您的 DataContext 中。

ItemCount is a property of whatever instance is contained in the DataContext property of the WPF object you want to change the FontSize of. In the hierarchy tree, everything from FrameworkElement onwards inherits the "DataContext" property.

With "snoop" you can look into the UI tree of a WPF App at runtime and e.g. figure out what objects live in your DataContext at any given time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文