Search code examples
wpfgridalignmentlabel

How to glue 2 labels together WPF


I am working on small wpf application and I can say this is my first wpf application from the beginning. I have problem here because I want to glue two labels together (see image below), because when I run app on smaller or bigger monitor they will be too much away from each other, and what I Want to do is to keep them together all the time and verticaly centered, so thats reason why I created grid and why I put stack panel inside, so maybe I could apply verticalalignment = 'center' to stack panel and content would be centered or whatever?

I am not sure is this code ok, so please guys comment it, I want to improve my skills about WPF, so be free to tell me another solutions or whatever, maybe I wrote too much code or smth?

Anyway, how could I glue up this two labes to keep them near each other all the time and to keep them also centered all the time on different size of monitors.

Thanks a lot guys, Cheers!

enter image description here

MY CODE:

<Window x:Class="xTouchPOS.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" WindowState="Maximized" WindowStyle="None">
    <Grid>
   <!-- Definition of my Grid which contains 2 columns and 3 rows. -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3.5*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="65"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="0.143*" />
        </Grid.RowDefinitions>
         <!-- Added this rectangle to colour header of my Grid. -->
        <Rectangle Grid.ColumnSpan="3">
            <Rectangle.Fill>
                <SolidColorBrush Color="#0091EA"></SolidColorBrush>
            </Rectangle.Fill>
        </Rectangle>
        <!-- Added this grid and stackpanel inside this grid to place date and time, but how to glue them together text and value  -->
        <Grid Grid.Column="1" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" Orientation="Vertical">
            <Label x:Name="lblTimeText"  Content="Time"  Margin="0,0,0,0"  FontSize="15" Foreground="White" HorizontalAlignment="Left" FontFamily="Arial" VerticalAlignment="Bottom" />
            <Label x:Name="lblTime" Content="labelTime" Grid.Column="0" Margin="0"  FontSize="18" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial"  />
        </StackPanel>

        <StackPanel Grid.Column="1" Orientation="Vertical">
            <Label Name="lblDateText" Content="Date" Margin="0" FontSize="15" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial" />
            <Label Name="lblDate"  Content="labelaDate" Margin="0" FontSize="18" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial"  />
        </StackPanel>

        </Grid>
    </Grid>
</Window>

CODE BEHIND:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            lblDate.Content = DateTime.Now.Date.ToString("MM/dd/yyyy");
            lblTime.Content = DateTime.Now.ToString("HH:mm:ss");
        }
    }

Solution

  • If you change your Labels to TextBlocks, I think you will get what you are looking for. You will need to change the column definition.

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <!-- Definition of my Grid which contains 2 columns and 3 rows. -->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1.75*"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" MinHeight="65"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="0.143*" />
            </Grid.RowDefinitions>
            <!-- Added this rectangle to colour header of my Grid. -->
            <Rectangle Grid.ColumnSpan="3">
                <Rectangle.Fill>
                    <SolidColorBrush Color="#0091EA"></SolidColorBrush>
                </Rectangle.Fill>
            </Rectangle>
            <!-- Added this grid and stackpanel inside this grid to place date and time, but how to glue them together text and value  -->
            <Grid Grid.Column="1" Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0" Orientation="Vertical" VerticalAlignment="Center">
                    <TextBlock x:Name="lblTimeText"  Text="Time"  Margin="0,0,0,0"  FontSize="15" Foreground="White" HorizontalAlignment="Left" FontFamily="Arial" VerticalAlignment="Bottom" />
                    <TextBlock x:Name="lblTime" Text="labelTime" Grid.Column="0" Margin="0"  FontSize="18" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial"  />
                </StackPanel>
    
                <StackPanel Grid.Column="1" Orientation="Vertical" VerticalAlignment="Center">
                    <TextBlock Name="lblDateText" Text="Date" Margin="0" FontSize="15" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial" />
                    <TextBlock Name="lblDate"  Text="labelaDate" Margin="0" FontSize="18" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial"  />
                </StackPanel>
    
            </Grid>
        </Grid>
    </Window>
    

    Alternative If you want to slim down your XAML, this will give the same result. It will also lock the two stack panels to the top right. Replace your Second Grid with this block

     <DockPanel Grid.Row="0" Grid.ColumnSpan="2">      
                <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Right" DockPanel.Dock="Right" Margin="5,0">
                    <TextBlock Name="lblDateText" Text="Date" Margin="0" FontSize="15" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial" />
                    <TextBlock Name="lblDate"  Text="labelaDate" Margin="0" FontSize="18" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial"  />
                </StackPanel>
                <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Right" DockPanel.Dock="Right" Margin="5,0">
                    <TextBlock x:Name="lblTimeText"  Text="Time"  Margin="0,0,0,0"  FontSize="15" Foreground="White" HorizontalAlignment="Left" FontFamily="Arial" VerticalAlignment="Bottom" />
                    <TextBlock x:Name="lblTime" Text="labelTime" Grid.Column="0" Margin="0"  FontSize="18" Foreground="White" HorizontalAlignment="Left"  FontFamily="Arial"  />
                </StackPanel>
            </DockPanel>