首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让数据绑定的WPF列表框生成子类ListboxItems

让数据绑定的WPF列表框生成子类ListboxItems
EN

Stack Overflow用户
提问于 2009-10-17 10:44:51
回答 1查看 393关注 0票数 2

我想让我的WPF列表框,这是数据绑定,生成子类ListboxItems而不是常规的ListboxItems。在本例中,DataTemplate是不够的,因为我需要为子类化的ListBoxItems提供一些自定义属性。

有没有办法让ListBox为绑定数据生成mySubClassedListBoxItem项?

谢谢,巴特

EN

回答 1

Stack Overflow用户

发布于 2011-06-14 07:52:22

您需要创建自己的ListBox子类,以便可以覆盖创建容器的方法,例如

代码语言:javascript
复制
public class MyListBox : ListBox
{
    public MyListBox()
    {
        // Should get the default style & template since styles are not inherited
        Style = FindResource(typeof(ListBox)) as Style;
    }

    protected override DependencyObject GetContainerForItemOverride()
    {
        var container = new MyListBoxItem();
        return container;
    }
}

public class MyListBoxItem : ListBoxItem
{
    public MyListBoxItem()
    {
        Style = FindResource(typeof(ListBoxItem)) as Style;
        // To easily see that these are custom ListBoxItems:
        // TextElement.SetForeground(this, Brushes.Red);
    }

    // ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1581934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档