Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Canvas, how to add children dynamically with MVVM code behind

Requirement:

To draw one Bitmap Image and rectangle(s) based on the collection of points. The rectangle should exactly fit on the pixels location over the image. There is also some text need to be added inside the rectangle.

The Image will be always only one and the rectangles will be dynamically added.

Current Solution:

Have a canvas with Image Control. Add the the dynamic code under the code behind file ViewImageResult.xaml.cs.

    private void DrawResult(int left, int right, int width, int height)     {         Border bord = new Border();         bord.BorderThickness = new Thickness(1);         bord.BorderBrush = Brushes.Red;         bord.Width = width;         bord.Height = height;         _mainCanvas.Children.Add(bord);         Canvas.SetLeft(bord, left);         Canvas.SetTop(bord, right);     } 

Issue:

Since i follow MVVM pattern, the collection of points for rectangle is in my ViewModel file ViewImageResultModel.cs. I am not able to add the child rectangle dynamically from the ViewModel file.

like image 629
Sathya Ram Avatar asked May 06 '11 15:05

Sathya Ram


2 Answers

ItemsControl is your friend:

<Grid>     <Image Source="..."/>     <ItemsControl ItemsSource="{Binding Points}">         <ItemsControl.ItemsPanel>             <ItemsPanelTemplate>                 <Canvas/>             </ItemsPanelTemplate>         </ItemsControl.ItemsPanel>         <ItemsControl.ItemContainerStyle>             <Style>                 <Setter Property="Canvas.Left" Value="{Binding X}"/>                 <Setter Property="Canvas.Top" Value="{Binding Y}"/>             </Style>         </ItemsControl.ItemContainerStyle>         <ItemsControl.ItemTemplate>             <DataTemplate>                 <Border BorderBrush="Red" BorderThickness="1" Width="{Binding Width}" Height="{Binding Height}"/>             </DataTemplate>         </ItemsControl.ItemTemplate>     </ItemsControl> </Grid> 

The above assumes your VM exposes a collection of points via a Points property, and that each point VM has X, Y, Width, and Height properties.

like image 55
Kent Boogaart Avatar answered Sep 21 '22 15:09

Kent Boogaart


Added IsItemsHost="True" to the Canvas of Kent's solution:

<Grid>     <Image Source="..."/>     <ItemsControl ItemsSource="{Binding Points}">         <ItemsControl.ItemsPanel>             <ItemsPanelTemplate>                 <Canvas  IsItemsHost="True"/>             </ItemsPanelTemplate>         </ItemsControl.ItemsPanel>         <ItemsControl.ItemContainerStyle>             <Style>                 <Setter Property="Canvas.Left" Value="{Binding X}"/>                 <Setter Property="Canvas.Top" Value="{Binding Y}"/>             </Style>         </ItemsControl.ItemContainerStyle>         <ItemsControl.ItemTemplate>             <DataTemplate>                 <Border BorderBrush="Red" BorderThickness="1" Width="{Binding Width}" Height="{Binding Height}"/>             </DataTemplate>         </ItemsControl.ItemTemplate>     </ItemsControl> </Grid> 
like image 42
Sathya Ram Avatar answered Sep 20 '22 15:09

Sathya Ram