Canvas binding

I have a canvas property in my class and I was wondering if it is possible to associate this with a canvas in xaml?

How will data binding on canvas work?

<Canvas ItemSource="{Binding ClassCanvas}" />
+3
source share
2 answers

If you want your Canvas defined in XAML to include the entire Canvas in your class as a separate element that you can write:

<Canvas>
  <ContentPresenter Content="{Binding ClassCanvas}" />
  ... other items here ...
</Canvas>

If you want your Canvas defined in XAML to include all the same UIElements as Canvas defined in your class, this is not possible because a UIElement can have only one UIElement parent. So, if a Canvas defined in a class is the parent of a given UIElement, a Canvas defined in XAML cannot be.

, Canvas UIElement Canvas, , ItemsControl Canvas DataTemplate:

<ItemsControl ItemsSource="{Binding ClassCanvas.Children}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Canvas />
    </ItemsPanelTemplate>
  </ItemsControl>
  <ItemsControl.ItemsContainerStyle>
    <Style>
      <Setter Property="Canvas.Left" Value="{Binding (Canvas.Left)}" />
      <Setter Property="Canvas.Left" Value="{Binding (Canvas.Top)}" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate>
            ... display of UIElement here, either using VisualBrush or custom display
          </ControlTemplate>
        <Setter.Value>
      </Setter>
    </Style>
  </ItemsControl.ItemsContainerStyle>
</ItemsControl>

, Children , INotifyCollectionChanged.

, UIElements Canvas.Top Canvas.Left, , ObservableCollection Canvas.

Canvas . ObservableCollection Canvas .

+3

.

.

0

Source: https://habr.com/ru/post/1752127/


All Articles