Best practice for closing tags in WPF

I am currently reading Pro WPF book in C # 2008 and I am new to WPF.

I basically have questions regarding best practice, since I want to write code that is usually accepted by other developers.

Throughout the book, I see that the author uses different ways to close tags for different purposes. I will give an example:

When creating a grid and defining rows and columns, it always writes this code, similar to this:

<Grid.ColumnDefinitions>
  <ColumnDefinition />
  <ColumnDefinition />
</Grid.ColumnDefinitions>

Thus, it closes the inline tag inside the tag, since the tag has no content (I'm not sure if the line is the correct wording for this)

But in most other places, it will add a closing tag, even if it does not contain such content

<TextBlock Text="{Binding Path=CategoryName}"></TextBlock>

, - ColumnDefinition, ,

<ColumnDefinition Width="Auto"></ColumnDefinition>

. ? , , , .

, .

+3
4

XML, WPF, , XML. , "Empty elements" (< tag/ > ), , .

, ,

+1

, .

,

<TextBlock Text="{Binding Path=CategoryName}"></TextBlock>

, :

<TextBlock Text="{Binding Path=CategoryName}">
    some other xaml
</TextBlock>

" xaml" ( - )

, ( - ).

+2

-, . , , , <keyword></keyword>. , , , . , , - .

0

When adding a control that has no elements, it is better to close it with a self-closing tag "/"> "instead of a hard closing tag.

eg.

<Label> 
    <Label.Content> 
        <TextBlock> 
            Test! 
        </TextBlock> 
    </Label.Content> 
</Label> 


<TextBlock Text={Binding Name}/>
0
source

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


All Articles