Why does WPF use attached properties for things like grid positioning?

Why do we need "attached properties"? The concept of this makes me a little difficult, since you can probably set property values ​​that do not even exist on a specific DependencyObject (and they will simply be ignored). It seems like a solution that is looking for a problem - why not just do something, for example. HTML does, and does the parent element explicitly define things like positioning for children?

That is, instead of:

<Grid>
  <Grid.ColumnDefinitions>
    <!-- etc. -->
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <!-- etc. -->
  </Grid.RowDefinitions>
  <SomeElement Grid.Column="0" Grid.Row="0" />
  <!-- etc. -->
</Grid>

Why not something like this (equivalent to <tr>and <td>in the HTML):

<Grid>
  <Grid.Row>
    <Grid.Column>
      <SomeElement />
    </Grid.Column>
    <!-- etc. -->
  </Grid.Row>
</Grid>

Perhaps grids are just a bad example, and the attached properties make more sense in other contexts? Or maybe I missed something at all?

+3
5

, , , :

<Grid.ItemContainerStyle>
   <Style TargetType="ContentPresenter">
      <Setter Property="Grid.Row" Value="{Binding Row}"/>
      <Setter Property="Grid.Column" Value="{Binding Column}"/>
   </Style>
</Grid.ItemContainerStyle>

HTML, , / .

+6

WPF Sells Griffiths: ( )

, FrameworkElement, Dock - WPF FrameworkElement, - -. DockPanel , . . , - , , - ? , .

. , "" . DockPanel Dock, . XAML (DockPanel.Dock) ,

+2

, , XAML , HTML . HTML .

+2

, . UIElement , (Row, Column, Dock, Top, Left...). , "" .

+2

TextBox , Row Column, , - .

Thus, they are implemented as attached properties. You can “attach” them as needed. If it is in a panel like DockPanel, these attached properties will make sense, otherwise they will not.

I hope I got a little confused.

0
source

All Articles