Binding to the pixel value of a star-width column-definition - wpf

I encounter a problem with the Width of a ScrollViewer in a column of a Grid.
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="{Binding GridSplitter, Mode=OneWay, FallbackValue=5}"/>
<ColumnDefinition Width="{Binding TableSize, Mode=TwoWay, FallbackValue=Auto}" />
</Grid.ColumnDefinitions>
The ScrollViewer is in Column[0] but does not always stretch its Width to the Width of Column[0]. Therefore I would like to give it just a Binding to the Pixel-value of the Width of ColumnDefintion[0].
Note that ColumnDefinitions[0].Width.Value = 1 because it's defined as a Star-value.
Also note that binding to ColumnDefinitions[0].ActualWidth gives a Width of 0.
Is it possible?

You can bind to the ActualWidth value, like so:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ActualWidth, ElementName=col1}"/>
<ColumnDefinition Width="*" Name="col1"/>
</Grid.ColumnDefinitions>

Related

How to make a TextBlock resize horizontally in a LsitBox

I've got a window with a ListBox containing TextBlocks in the template:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
...
<ListBox x:Name="AnnotationsList" ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.Row="1" Grid.ColumnSpan="3">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="20*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding DateAdded}" Grid.Column="0"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I expected the TextBlock to adjust the size, with both elements in the template filling the width together in 1/20 proportions.
Instead, the TextBlock never gets smaller and ListBox gets a horizonal scrollbar.
How can I keep the list scrollable vertically and make sure the text gets narrower and wraps if I resize the window?
As Clemens answered in the comments, HorizontalScrollBarVisibility has an option of Disabled, which doesn't affect just the visibility, but actually triggers the children elements to get limited by the horizontal size of the container.

How to make binded grid size to be proportionate value?

<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!

Bind MaxWidth to calculated Auto value

I have a datatemplate containing this section. The second column is used for a GridSplitter.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MaxWidth="100"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid>
What I am trying to do is to bind the MaxWidth of the first column to the width of its content instead of a hardcoded width value.
The idea is that the user can make the column narrower than its content but not wider.
Is this possible with simple binding?
OneTime Binding to Actual Width of the content UserControl did the trick!
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MaxWidth="{Binding ActualWidth, ElementName=ColumnContent, Mode=OneTime}" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid>

Auto Sizing between grid columns

I have two controls and a GridSplitter .
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<UserControlOne Grid.Colum="0" Visibility="{Binding MyProperty1}"/>
<GridSplitter Visibility="{Binding MyProperty1}" m:Splitterbehaviour.Apply= true/>
<UserControlTwo Grid.Colum="1" />
</Grid>
I am trying to show/hide the UserControlOne with the MyProperty1 which is working fine but when it is hidden i want the UsercontrolTwo to take whole page space. I could easily achieve this by using a stack or dock panel. But if i use the stackpanel or dockpanel my GridSplitter wont work.(I have a behaviour set to GridSplitter which will identify the first column and it will help to resize the first and second column)
I don't see how that splitter is working
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<UserControlOne Grid.Colum="0" Visibility="{Binding MyProperty1}"/>
<UserControlTwo Grid.Colum="1"/>
</Grid>

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