How to split Grid 2x5 and display same pattern in XAML

JeffinAtl 161 Reputation points
2021-06-03T03:20:22.99+00:00

I have a grid and want to display 2x5 child grid in XAML.
I made main grid and defined 2 raws and 5 columns.
I'd like to display information(Chart#, Patient Name, Phone# and Type) in each cell.
Can I make a Style and use it from each cells?

Developer technologies | Windows Presentation Foundation
{count} votes

Accepted answer
  1. DaisyTian-1203 11,646 Reputation points
    2021-06-04T06:20:29.503+00:00

    You could use ListBox/DataGrid and create its ItemTemplate like EmonHaque-1485 said. If you must use Grid to implement, you can use below code:

        <Window.Resources>  
            <ControlTemplate x:Key="MyTemplate">  
                <Grid>  
                    <Border BorderThickness="7" CornerRadius="4">  
                        <Border.BorderBrush>  
                            <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>  
                        </Border.BorderBrush>  
                        <Grid>  
                            <Grid.Background>  
                                <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>  
                            </Grid.Background>  
                            <Grid.ColumnDefinitions>  
                                <ColumnDefinition Width="40"></ColumnDefinition>  
                                <ColumnDefinition Width="40"></ColumnDefinition>  
                                <ColumnDefinition Width="40"></ColumnDefinition>  
                                <ColumnDefinition Width="40"></ColumnDefinition>  
                                <ColumnDefinition Width="40"></ColumnDefinition>  
                            </Grid.ColumnDefinitions>  
                            <Grid.RowDefinitions>  
                                <RowDefinition Height="30"></RowDefinition>  
                                <RowDefinition Height="30"></RowDefinition>  
                            </Grid.RowDefinitions>  
                            <TextBlock Grid.Row="0"  Grid.Column="0" Text="Chart" ></TextBlock>  
                            <TextBlock Grid.Row="0"  Grid.Column="1" Text="Name" ></TextBlock>  
                            <TextBlock Grid.Row="0"  Grid.Column="2" Text="Phone" ></TextBlock>  
                            <TextBlock Grid.Row="0"  Grid.Column="3" Text="Type" ></TextBlock>  
                            <TextBlock Grid.Row="0"  Grid.Column="4" Text="col5" ></TextBlock>  
                            <TextBlock Grid.Row="1"  Grid.Column="0"  Background="LightBlue"></TextBlock>  
                            <TextBlock Grid.Row="1"  Grid.Column="1"  Background="LightBlue"></TextBlock>  
                            <TextBlock Grid.Row="1"  Grid.Column="2"  Background="LightBlue"></TextBlock>  
                            <TextBlock Grid.Row="1"  Grid.Column="3"  Background="LightBlue"></TextBlock>  
                            <TextBlock Grid.Row="1"  Grid.Column="4"  Background="LightBlue"></TextBlock>  
                        </Grid>  
                    </Border>  
                </Grid>  
            </ControlTemplate>  
            <Style TargetType="ContentControl">  
                <Setter Property="Template" Value="{StaticResource MyTemplate}"></Setter>  
            </Style>  
        </Window.Resources>  
        <Grid>  
            <Grid.ColumnDefinitions>  
                <ColumnDefinition Width="200"></ColumnDefinition>  
                <ColumnDefinition Width="200"></ColumnDefinition>  
                <ColumnDefinition Width="200"></ColumnDefinition>  
                <ColumnDefinition Width="200"></ColumnDefinition>  
                <ColumnDefinition Width="200"></ColumnDefinition>  
            </Grid.ColumnDefinitions>  
            <Grid.RowDefinitions>  
                <RowDefinition Height="70"></RowDefinition>  
                <RowDefinition Height="70"></RowDefinition>  
            </Grid.RowDefinitions>  
            <ContentControl Grid.Row="0"  Grid.Column="0" ></ContentControl>  
            <ContentControl Grid.Row="0"  Grid.Column="1" ></ContentControl>  
            <ContentControl Grid.Row="0"  Grid.Column="2" ></ContentControl>  
            <ContentControl Grid.Row="0"  Grid.Column="3" ></ContentControl>  
            <ContentControl Grid.Row="0"  Grid.Column="4" ></ContentControl>  
      
            <ContentControl Grid.Row="1"  Grid.Column="0" ></ContentControl>  
            <ContentControl Grid.Row="1"  Grid.Column="1" ></ContentControl>  
            <ContentControl Grid.Row="1"  Grid.Column="2" ></ContentControl>  
            <ContentControl Grid.Row="1"  Grid.Column="3" ></ContentControl>  
            <ContentControl Grid.Row="1"  Grid.Column="4" ></ContentControl>  
        </Grid>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.