【发布时间】:2015-06-10 17:39:52
【问题描述】:
我一直在试图弄清楚如何将ObservableCollection<FrameworkElements> 绑定到 ItemsControl。我有一个现有项目,它严重依赖背后的代码和没有绑定的画布,我正在尝试更新它以使用 mvvm 和 prism。
ObservableCollection 将填充许多 Path 项。它们是从我使用的外部库生成的。当我手动操作画布本身时,库可以正常工作。
这是来自 ViewModel 的代码的 sn-p:
ObservableCollection<FrameworkElement> _items;
ObservableCollection<FrameworkElement> Items
{
get { return _items; }
set
{
_items = value;
this.NotifyPropertyChanged("Items");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
支持 XAML
<ItemsControl ItemsSource="{Binding Items}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="canvas" IsItemsHost="True">
<Canvas.Background>
<SolidColorBrush Color="White" Opacity="100"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Path/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我遇到的问题是路径从不绘制。关于我哪里出错以及从哪里开始调试过程有什么建议吗?
【问题讨论】:
-
原始实例有效,我只是在声明集合时忘记了
public。