Grid columns shows 1px if all is set to Auto - wpf

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.

Related

Radio Button Navigation issue in WPF?

I have Loaded 3 radio button inside the Grid panel using the column definition(each with in a specific column) On this time i have noticed that the arrow key navigation is not properly Working. But the Tab and shift+tab Navigation are working as per the expectations. That the same is working fine while loading the radio button inside the stack panel. After the various inspection i have noticed that the issue occurs while Using Horizontal alignment property of the Radio button as Left. It give the same odd behavior for all the option except stretch.Can anyone describe how to resolve this issue.
I have tried applying of Group Name to the Radio Button also
<Grid Grid.Row="0" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="12"/>
</Grid.ColumnDefinitions>
<RadioButton Content="Automatic" Grid.Column="1"
HorizontalAlignment="Stretch"
IsChecked="{Binding Path=SelectedSizeType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Apple}"/>
<RadioButton Content="Manual" Grid.Column="3"
HorizontalAlignment="Stretch"
IsChecked="{Binding Path=SelectedSizeType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Mango}"/>
<RadioButton Content="Customizable" Grid.Column="5" x:Name="Customizable"
HorizontalAlignment="Stretch"
IsChecked="{Binding Path=SelectedSizeType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Carrot}"/>
</Grid>
You can try to set the attached property KeyboardNavigation.DirectionalNavigation on the grouping container element to force a certain navigation behavior according to it's KeyboardNavigationMode value.
The following example sets KeyboardNavigation.DirectionalNavigation to KeyboardNavigationMode.Local on the containing Grid:
<Grid KeyboardNavigation.DirectionalNavigation="Local" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="12"/>
</Grid.ColumnDefinitions>
<RadioButton Content="Automatic" Grid.Column="1"
HorizontalAlignment="Left" />
<RadioButton Content="Manual" Grid.Column="3"
HorizontalAlignment="Left" />
<RadioButton Content="Customizable" Grid.Column="5" x:Name="Customizable"
HorizontalAlignment="Left" />
</Grid>

WPF style for Grid with defined columns and rows? [duplicate]

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>

Grid + splitter

<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" />

WPF: getting AccessText in ScrollViewer to wrap

I have a Grid containing a ScrollViewer containing AccessText. I want the AccessText to take up the full width of the ScrollViewer, which should take up the full width of the Grid, without any overflow. Currently, the contents of my AccessText are cut off on the right side of the screen instead of wrapping. I have tried setting AccessText.TextWrapping to Wrap, WrapWithOverflow, and I've also tried removing the property entirely. I switched to using a Grid instead of a StackPanel because I thought that might affect how the contents are sized, but that hasn't helped. Here's what I have:
<Grid MaxHeight="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Whee a label:" Grid.Column="0"/>
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
<AccessText Text="{Binding MyLongTextField}"/>
</ScrollViewer>
</Grid>
When you set ColumnDefinition Width to Auto, the ScrollViewer within it won't be limited by the "visible Width" of the Column, so it will still take up as much horizontal space as it needs. With the xaml you posted, I think Width="*" will suit your needs. For the ScrollViewer, it seems like you don't want it to be able to Scroll horizontaly but only verticaly? In that case, set HorizontalScrollBarVisibility="Disabled". Otherwise you'll get a Horizontal ScrollBar.
<Grid MaxHeight="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Whee a label:" Grid.Column="0"/>
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<AccessText TextWrapping="Wrap" Text="{Binding MyLongTextField}"/>
</ScrollViewer>
</Grid>
If you simply want the AccessText to wrap indefinitely, modify your second ColumnDefinition from Auto to * and move the AccessText outside of the ScrollViewer as seen below...
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Whee a label:" Grid.Column="0"/>
<AccessText Grid.Column="1" TextWrapping="Wrap" Text="{Binding MyLongTextField}"/>
</Grid>
The reason the text would not wrap is because the second ColumnDefinition was set to Auto; which essentially does not force a bounds around the AccessText.
If you want to keep the ScrollViewer try this...
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Whee a label:" Grid.Column="0"/>
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
<AccessText TextWrapping="Wrap" Text="{Binding MyLongTextField}"/>
</ScrollViewer>
</Grid>
This ended up giving me what I wanted:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Whee a label:" Grid.Column="0"/>
<ScrollViewer Grid.Column="1" MaxHeight="40"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<AccessText Text="{Binding CRData.Error}" TextWrapping="Wrap"/>
</ScrollViewer>
</Grid>
Thanks to Meleak and Aaron for the suggestion of using * for the column width instead of Auto, and to Meleak for suggesting Disabled for the horizontal scrollbar instead of Auto.

ColumnDefinition MinWidth doesn't work correctly

I'm using a Grid in WPF (xaml) and I'm have some strange effect when using the MinWidth property in a ColumnDefinition. For example, when I use 9 ColumnDefinition and every ColumnDefinition has the 'Width="*"' property and one of the middle columns also has a MinWidth property then the size of the other columns is wrong.
Well, it's hard to discribe but this xaml code illustrates it nicely:
<Grid Width="500">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*" MinWidth="250"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="Green"/>
<Border Grid.Column="1" Background="Blue"/>
<Border Grid.Column="2" Background="Red"/>
<Border Grid.Column="3" Background="Yellow"/>
<Border Grid.Column="4" Background="Purple"/>
<Border Grid.Column="5" Background="Orange"/>
<Border Grid.Column="6" Background="Azure"/>
<Border Grid.Column="7" Background="LightBlue"/>
<Border Grid.Column="9" Background="LightGreen"/>
</Grid>
When you run this xaml code you'll see that the first 3 columns have a different width than the last 5 columns. Where I expected all of those to have the same width.
Does anyone know if this is a bug. And if there is a way to do this correctly.
Thanks in advance.
I see what you mean - the columns to the left of the yellow one are wider than the columns to the right, even though they are meant to be given the same proportions.
I would say it's a bug, especially when you consider that the following workaround works:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="1.0000001*" MinWidth="250"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
I would guess that this bug is related to how the columns are grouped by width...
It looks like it just the way it works. You've limited grid in 500 points, and said: hey, give all grid columns the same width, but also this column should be at least 250 points. Now the question from WPF to you: Dude, I see you asked me to give each of 9 column at least 250 points, how can I do this in 500 points? And it makes a decision, to respect your minimum width, but the price is - width of the rest columns.
As for the way to do this correctly. What do you mean? What do you want?

Resources