Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF grid column def auto always clipping from right

Tags:

c#

wpf

I have a WPF app which has a grid with 2 columns set to * and auto. The issue is when I reduce the size of the window the children in second column are getting clipped from right instead of left. I expect them to clip from left because I have set the horizontal alignment to right. Is there a way we can clip the second column elements from left?

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="60"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <DockPanel  HorizontalAlignment="Right" Grid.Column="1">
            <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
            <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
            <Label x:Name="mLog"/>
        </DockPanel>
        <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>
        <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
    </Grid>
</Window>

enter image description here

like image 917
puneet Avatar asked Jul 22 '19 06:07

puneet


People also ask

What is the difference between * and Auto in WPF?

Auto means that a column is given as much width as the elements within it require. The width of * sized columns is calculated by allocating space for the Auto , and fixed width columns, and then dividing up the remaining space.

What is grid splitter in WPF?

WPF GridSplitter control is a divider that helps to split the available space into rows and columns in a grid. It supports to splits the controls horizontally or vertically with a splitter. In addition to that, it allows users to resize the grid's column width and row height on demand.

What is grid row in WPF?

A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties. By default, a Grid panel is created with one row and one column.

What is Grid WPF?

A Grid is a very powerful and useful Layout in WPF. It enables you to arrange children elements in cells defined by rows and columns. In fact, when we add a new XAML document or create a new WPF Project in Visual Studio, Visual Studio automatically adds a Grid as the first container inside the window element.


2 Answers

Instead of this:

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="60"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <DockPanel  HorizontalAlignment="Right" Grid.Column="1">
            <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
            <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
            <Label x:Name="mLog"/>
        </DockPanel>
        <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>
        <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
    </Grid>
</Window>

try this:

<Window x:Class="WpfApp2.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:WpfApp2"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MinWidth="60"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <DockPanel  HorizontalAlignment="Right" Grid.Column="1">
                <Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
                <Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
                <Label x:Name="mLog"/>
            </DockPanel>
            <Button Click="Button_Click" DockPanel.Dock="Right" Content="click me"/>
            <Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
        </Grid>
    </Window>

i deleted the last button width property.

output: wpf grid

like image 190
AmRo Avatar answered Oct 11 '22 08:10

AmRo


I would guess you are trying to make 3 column layout with 1 button on either side and 1 middle lane for content.

So you could try something like this, where all your content are in same grid with different z-index and horizontal alignments, now when you resize the window the label stays in middle and the buttons "clip behind" the label content from the right and the left.

<Grid>
    <Button HorizontalAlignment="Left" Content="click me" Width="150" />
    <Button HorizontalAlignment="Right" Content="click me" Width="150" />
    <Label HorizontalAlignment="Center" Content="abcdef" Width="200" Background="White" />
</Grid>
like image 25
Janne Matikainen Avatar answered Oct 11 '22 10:10

Janne Matikainen