我想让我的WPF列表框,这是数据绑定,生成子类ListboxItems而不是常规的ListboxItems。在本例中,DataTemplate是不够的,因为我需要为子类化的ListBoxItems提供一些自定义属性。
有没有办法让ListBox为绑定数据生成mySubClassedListBoxItem项?
谢谢,巴特
发布于 2011-06-14 07:52:22
您需要创建自己的ListBox子类,以便可以覆盖创建容器的方法,例如
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);
}
// ...
}https://stackoverflow.com/questions/1581934
复制相似问题