简体   繁体   中英

Create a type/tag in XAML/WPF

Here is a part of my XAML in one of my window.

 <Border BorderBrush="#a197ff" BorderThickness="1" Margin="5 5" Grid.Row="3">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="10*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>

                <Image MaxHeight="64" Source="/GamingClient;component/Assets/next.png" Grid.Column="2"/>
                <Image MaxHeight="64" Source="/GamingClient;component/Assets/prev.png" Grid.Column="0"/>

                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="7*" />
                    </Grid.ColumnDefinitions>

                    <Image  MaxHeight="215" MaxWidth="215" HorizontalAlignment="Center" Margin="5 0" Source="/Maps/1.jpg"/>
                    <Grid Grid.Column="1">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="4*" />
                        </Grid.RowDefinitions>

                        <ComboBox Name="comboBoxMapsName" SelectionChanged="comboBoxMapsName_SelectionChanged" VerticalAlignment="Center" HorizontalAlignment="Left" MaxWidth="200"  Margin="5 0" Grid.Row="0"/>
                        <TextBlock FontStyle="Italic" Foreground="White" Text="Description de la carte par l'auteur qui n'apporte aucune information pertinente aux yeux du joueur ou de son adversaire qui lui est aussi pourri que l'hôte."  TextWrapping="Wrap" Margin="10 5 5 5" Grid.Row="1"/>
                    </Grid>
                </Grid>

            </Grid>
        </Border>

This ''module'' is going to be ''repeated'' elsewhere, caring about good software practices I was wondering if there was any way of ''creating'' a Type/Tag in XAML. IE so I could write

<CustomXAMLObject><CustomXAMLObject/>

and it would "call" the code I displayed.

Furthermore, once I have this "XAML" module, is it possible to pair it with some C# logic so that I get a "View/Controller" package?

Since I'm always a glutton for easy points, I suppose I'll go ahead and make an actual answer post. :)

In WPF and other .NET technologies you have the ability to create a templated collection of controls for re-usability and better organization called a UserControl that inherits from ContentControl allowing you to place multiple bits of content into one reference-able control for use throughout your application.

This way you're not reiterating the same code over and over and when you need to change something you can do so in one centralized place that will reflect globally wherever that control is used.

Hope this helps, cheers. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM