我一直在想如何将ObservableCollection<FrameworkElements>
绑定到ItemsControl。我有一个现有的项目,严重依赖代码和画布的没有绑定,我试图更新使用mvvm和棱镜。将Shapes.Path项绑定到ItemsControl
ObservableCollection将被填充许多Path项。它们是从我使用的外部图书馆生成的。当我手动操作画布本身时,库正常工作。
下面是从视图模型的代码片段:
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'。 –