This question already has answers here:
Sharing Column Widths Between Multiple WPF Controls
(3 answers)
Closed 3 years ago.
In my WPF app I have 2 grids inside a parent grid:
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="col1 width"/>
<ColumnDefinition Width="col2 width"/>
<Grid.ColumnDefinitions>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" x:Name="col1"/>
<ColumnDefinition Width="Auto" x:Name="col2"/>
<Grid.ColumnDefinitions>
</Grid>
</Grid>
How can I bind the width of the column definitions in the first grid to the other grid?
You can do this
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=col1, Path=Width}"/>
<ColumnDefinition Width="{Binding ElementName=col2, Path=Width}"/>
<Grid.ColumnDefinitions>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" x:Name="col1"/>
<ColumnDefinition Width="Auto" x:Name="col2"/>
<Grid.ColumnDefinitions>
</Grid>
</Grid>
I tested the below code and it works well.
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}"/>
<ColumnDefinition Width="{Binding ElementName=col2, Path=ActualWidth}"/>
</Grid.ColumnDefinitions>
</Grid>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" x:Name="col1"/>
<ColumnDefinition Width="150" x:Name="col2"/>
</Grid.ColumnDefinitions>
</Grid>
Related
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Width1}*" />
<ColumnDefinition Width="{Binding Width2}*" />
<ColumnDefinition Width="{Binding Width3}*" />
</Grid.ColumnDefinitions>
I basically want whatever the value of Width1, Width2 ... to have an asterisk after it.
So let's say the Width1 is 5, I want it to be 5* so that it will be a proportionate value.
I'm fairly new too WPF myself but I would do something like this:
And then you can do your binding on the WidthValString:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding WidthValString}" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
I'm assumming you will do this with ColumnDefinitions because you cannot have a grid with a width of 5*. As far as I know it has to be an integer. (With ColumnDef/RowDefs you'll be fine)
If this is not what you want, please specify your question!
This question already has answers here:
How to create reusable WPF grid layout
(6 answers)
Closed 3 years ago.
In my application I have a lot of grids like this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- ... -->
</Grid>
They have 4 columns and 3 rows and column widths are set to some value.
Can I create style or some other kind of template to simplify my window that uses 20 grids like this?
I know, that I can create styles for column definitions one by one to avoid Width="10" everywhere, but is it possible to do something like this?
<Grid Style="{StaticResource GridWith4ColumnsAnd3Rows}">
<!-- ... -->
</Grid>
You can't set the RowDefinitions or ColumnDefinitions properties in a Style because they are not dependency properties.
But you could create a custom class that inherits from Grid and adds the ColumnDefinitions and RowDefinition in the constructor:
public class CustomGrid : Grid
{
public CustomGrid()
{
ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(10) });
//...
ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
}
}
Usage:
<local:CustomGrid>...</local:CustomGrid>
Alternatively, you could define an attached behaviour that creates the RowDefinitions and ColumnDefinitions.
Nice answer is here:
How to create reusable WPF grid layout
I have used <ItemsPanel> control with Grid inside.
Style (in my App.xaml):
<Style x:Key="SchedulerFieldGridStyle1" TargetType="ItemsControl" >
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid Background="LightSteelBlue" Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
Part of MainWindow.xaml before:
<Grid Background="LightSteelBlue" Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.ColumnSpan="4" Content="Day of week"/>
<Label Grid.Column="0" Grid.Row="1" Content="From"/>
<ComboBox Grid.Column="1" Grid.Row="1" />
<Label Grid.Column="2" Grid.Row="1" Content="To"/>
<ComboBox Grid.Column="3" Grid.Row="1" />
</Grid>
Part of MainWindow.xaml after:
<ItemsControl Style="{StaticResource SchedulerFieldGridStyle1}">
<Label Grid.ColumnSpan="4" Content="Day of week"/>
<Label Grid.Column="0" Grid.Row="1" Content="From"/>
<ComboBox Grid.Column="1" Grid.Row="1"/>
<Label Grid.Column="2" Grid.Row="1" Content="To"/>
<ComboBox Grid.Column="3" Grid.Row="1"/>
</ItemsControl>
Why the Column 1 and 3 shows 1px if is empty?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="Red"/>
<Border Grid.Column="1" Background="Blue">
<TextBlock Text="123"/>
</Border>
<Border Grid.Column="2" Background="Azure"/>
</Grid>
Is bug or something that i don't understand?
Is possible to make Auto and Hide the 1px.
Edit: Need UseLayoutRounding="True". I have on Window.
Add and extra: <ColumnDefinition Width="*"/> and works fine.
I have a structure of items like this for example:
<FixedDocument Grid.IsSharedSizeScope="True">
<PageContent>
<FixedPage>
<Grid Width="500" Height="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" SharedSizeGroup="A"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</FixedPage>
</PageContent>
<PageContent>
<FixedPage>
<Grid Width="500" Height="200">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</FixedPage>
</PageContent>
</FixedDocument>
Why doesn't width of first column of grids have a same value on different fixed pages?
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="100"/>
<ColumnDefinition Width="4"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Canvas Grid.Column="0" HorizontalAlignment="Stretch" Background="Orange"/>
<GridSplitter Grid.Column="1" Width="4" Background="Black" />
<Grid Grid.Column="2" />
When I resize the Canvas in column 0, the canvas is not stretched to fill its column.
The stretch doesn't seem to work.
When I use Width="*" (which I actually do not want) for the first column I canot move the spliter to the left.
My requirement is that the first column is resizable (with a minimum) and that the canvas fills the first column.
Try this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="100"/>
<ColumnDefinition Width="4"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Canvas Grid.Column="0" Grid.ColumnSpan="2" Background="Orange" />
<GridSplitter Grid.Column="1" Width="4" Background="Black" />
<Grid Grid.Column="2" Background="Green"/>
</Grid>
Grid.ColumnSpan="2" is the key.
You need to set HorizontalAlignment="Stretch" on Grid Splitter to get it work.
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="100"/>
<ColumnDefinition Width="4"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Canvas Grid.Column="0" HorizontalAlignment="Stretch" Background="Orange"/>
<GridSplitter Grid.Column="1" Width="4" Background="Black"
HorizontalAlignment="Stretch" /> <-- HERE
<Grid Grid.Column="2" />