Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to bind a Canvas's Children property in XAML?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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&lt;UIElement&gt; collection = sender as ObservableCollection&lt;UIElement&gt;; foreach (UIElement element in collection) if (!designerCanvas.Children.Contains(element)) designerCanvas.Children.Add(element); List&lt;UIElement&gt; removeList = new List&lt;UIElement&gt;(); foreach (UIElement element in designerCanvas.Children) if (!collection.Contains(element)) removeList.Add(element); foreach (UIElement element in removeList) designerCanvas.Children.Remove(element); } </code></pre> <p>I'd much rather just set up a binding in XAML like this:</p> <pre class="lang-xml prettyprint-override"><code>&lt;Canvas x:Name="designerCanvas" Children="{Binding Document.Items}" Width="{Binding Document.Width}" Height="{Binding Document.Height}"&gt; &lt;/Canvas&gt; </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload