I have the following viewmodel which I'd like to bind to a view:
public class EntityManagerModel
{
public readonly ObservableCollection<EntityViewModel> m_Entities =
new ObservableCollection<EntityViewModel>();
}
//
// implements INotifyPropertyChanged
public class EntityManagerViewModel: BaseViewModel
{
private EntityManagerModel m_Model = new EntityManagerModel();
public ObservableCollection<EntityViewModel> Entities
{
get { return m_Model.m_Entities; }
}
}
However what I actually would like to bind to my view is the data contained in EntityViewModel
which is defined like so:
public class EntityModel
{
public readonly PointCollection m_TrailPoints = new PointCollection();
}
//
// implements INotifyPropertyChanged
public class EntityViewModel: BaseViewModel
{
private EntityModel m_Model = new EntityModel;
public PointCollection TrailPoints
{
get { return m_Model.m_TrailPoints; }
}
}
So, I'd like my view to update every time I add a point like this:
// In some routine inside EntityManageViewModel I have this code
// this should trigger collectionChanged event:
Entities[index].TrailPoints.Add( new Point(x, y));
My view's XAML code is:
<!-- EntityManagerView -->
<Grid>
<!--Trail line-->
<ItemsControl ItemsSource="{Binding Entities}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:EntityViewModel}">
<Polyline Stroke="Red" StrokeThickness="1">
<Polyline.Style>
<Style TargetType="{x:Type Polyline}">
<Setter Property="Points">
<Setter.Value>
<MultiBinding Converter="{StaticResource pointMultiConverter}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Canvas}}"/>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Canvas}}"/>
<Binding Path="TrailPoints"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Polyline.Style>
</Polyline>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
This only displays/updates the lines when i resize the application window. Of course, I have found solutions like this while searching. But subclassing ObservableCollection
introduces other problems in my code. So, my question is what is the best/alternative way to do this?