wpfxamlsyntax

Could anyone explain this syntax of XAML?


Could anyone explain this syntax of XAML?

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Background="#dfe2e8"
    WindowStyle="None"
    Title="MainWindow" Height="450" Width="800">
<WindowChrome.WindowChrome>
    
</WindowChrome.WindowChrome>

Is WindowChrome a property of Window? If not, then how are we referencing it?

I understand how this works:

<Grid>
  <Grid.Background></Grid.Background>
 </Grid>

It's setting the background property of Grid


Solution

  • WindowChrome class has an attached property whose name is the same as the class itself and whose type is the class itself. In other words, WindowChrome is 1) the name of an attached property, 2) the name of the class where the attached property is defined, 3) the name of type of the attached propery. Please note that WindowChrome attached property is static but WindowChrome class is non-static and instantiable.

    One of the ways to write a syntax of an attached property is as follows:

    <[name of class where attached property is defined].[name of attached property]>
        <[name of type of attached property]/>
    </[name of class where attached property is defined].[name of attached property]>
    

    Therefore, in the case of WindowChrome, the syntax will be as follows:

    <WindowChrome.WindowChrome>
        <WindowChrome/>
    </WindowChrome.WindowChrome>
    

    If you wish to refer the instance of WindowChrome class stored in WindowChrome attached property, you can name the instance and use the name.

    <Window x:Class="WpfApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <WindowChrome.WindowChrome>
            <WindowChrome x:Name="chrome" CaptionHeight="80"/>
        </WindowChrome.WindowChrome>
    
        <Grid>
            <Button Margin="0,80,0,0"
                    Content="{Binding ElementName=chrome, Path=CaptionHeight}"/>
        </Grid>
    </Window>