Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bind a Canvas's Children property in XAML?

I'm a little surprised that it is not possible to set up a binding for Canvas.Children through XAML. I've had to resort to a code-behind approach that looks something like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e) {     DesignerViewModel dvm = this.DataContext as DesignerViewModel;     dvm.Document.Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged);      foreach (UIElement element in dvm.Document.Items)         designerCanvas.Children.Add(element); }  private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {     ObservableCollection<UIElement> collection = sender as ObservableCollection<UIElement>;      foreach (UIElement element in collection)         if (!designerCanvas.Children.Contains(element))             designerCanvas.Children.Add(element);      List<UIElement> removeList = new List<UIElement>();     foreach (UIElement element in designerCanvas.Children)         if (!collection.Contains(element))             removeList.Add(element);      foreach (UIElement element in removeList)         designerCanvas.Children.Remove(element); } 

I'd much rather just set up a binding in XAML like this:

<Canvas x:Name="designerCanvas"         Children="{Binding Document.Items}"         Width="{Binding Document.Width}"         Height="{Binding Document.Height}"> </Canvas> 

Is there a way to accomplish this without resorting to a code-behind approach? I've done some googling on the subject, but haven't come up with much for this specific problem.

I don't like my current approach because it mucks up my nice Model-View-ViewModel by making the View aware of it's ViewModel.

like image 458
Rob Avatar asked May 20 '09 19:05

Rob


People also ask

How does binding work in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

How many types of binding are there in WPF?

WPF binding offers four types of Binding.

How do I bind a list in WPF?

<ListView. View> <GridView> <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>

What is ItemsSource binding WPF?

ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed.


1 Answers

<ItemsControl ItemsSource="{Binding Path=Circles}">     <ItemsControl.ItemsPanel>          <ItemsPanelTemplate>               <Canvas Background="White" Width="500" Height="500"  />          </ItemsPanelTemplate>     </ItemsControl.ItemsPanel>     <ItemsControl.ItemTemplate>         <DataTemplate>             <Ellipse Fill="{Binding Path=Color, Converter={StaticResource colorBrushConverter}}" Width="25" Height="25" />         </DataTemplate>     </ItemsControl.ItemTemplate>     <ItemsControl.ItemContainerStyle>         <Style>             <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />             <Setter Property="Canvas.Left" Value="{Binding Path=X}" />         </Style>     </ItemsControl.ItemContainerStyle> </ItemsControl> 
like image 188
kenwarner Avatar answered Oct 11 '22 23:10

kenwarner