【发布时间】:2010-10-29 12:56:45
【问题描述】:
我正在开发一个 ListBox,它会覆盖其 ItemsPanelTemplate 以使用 Canvas 而不是 StackPanel。 ListBoxItems 有一个 DataTemplate,它使用转换器来定义每个 ListBoxItem 在画布上的外观和位置。当我将一个项目添加到ListBox 绑定到的集合中时,我希望能够将其他UIElements 添加到画布中。我可以在ListBoxItem 的转换器之外完成此操作吗?
我的资源部分是这样的:
<Style TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Canvas x:Key="cnvAwesome">
</Canvas>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Left" Value="{Binding Path=Position.X}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Position.Y}" />
</Style>
还有一个列表框:
<ListBox x:Name="lstAwesome" ItemsSource="{Binding Source={StaticResource someCollection}}"/>
还有ListBox绑定的集合:
public ObservableCollection<SomeType> someCollection {get; set; }
所以,如果我有一种方法可以将项目添加到 ListBox lstAwesome 绑定到的集合 someCollection 中:
private void AddItemToDataboundCollection(SomeType someItem)
{
someCollection.Add(someItem)
// I'd like to add a UIElement to the canvas that the ListBox uses to layout items here.
}
那么,我可以将 UIElements 添加到数据绑定列表框用于 ItemsPanel 的画布中吗?以编程方式,当我将项目添加到 ListBox 绑定到的集合中时?
非常感谢任何意见!
【问题讨论】:
标签: wpf data-binding listbox uielement