Search code examples
wpfattached-properties

Why is Grid.Row attached property specified without any enclosing Grid?


I am reading code from a WPF project which has the following XAML code (I have omitted the boilerplate parts):

<Window x:Class="AdornedControlSample.Window1"
    ...
    x:Name="window"
    ...
    >
    <Canvas
        Grid.Row="1"
        x:Name="canvas"
        >
    ...

Please note that there is no Grid defined whatsoever anywhere in this XAML file. So, my questions are:

  1. What is the purpose of the Grid.Row="1" within the Canvas definition?
  2. What purpose does x:Name="window" serve? I have not seen anything in the code-behind that references a window. (There are references to Window1, though.)

Solution

  • Question 1

    In your example, Grid.Row has no purpose at all. You gave it a value, but that value will not be used because there is no Grid around the Canvas. It also doesn't give an error because it is a valid property/value. The RowProperty is defined in the Canvas class.

    Question 2

    It gives a unique name (within the window itself) to the UI-element. For now, it has no purpose. So you can remove it safely. However, if you want to access the window in the code-behind of the view, the x:name serves a purpose. But you can also access the window via the this keyword.