Bind MaxWidth to calculated Auto value - wpf

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>

Related

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!

UserControl cropped when used in MainWindow

Hello Stackoverflowers !
I'm creating a list with a searchbar in XAML, and when i tried to use my new UserControl in MainWindow.xaml, the Button in the UserControl is cropped in the designer instead of being resized to fit, why is that ?
Here is the MainWindow.xaml body :
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions>
<controls:SearchBox Grid.Column="0"/>
</Grid>
And here is the body of the custom control :
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="8*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox
Grid.Row="0"
VerticalAlignment="Center"
Text="Search"
TextBlock.FontStyle="Italic"
TextBlock.TextAlignment="Left"
/>
<!-- Content="{StaticResource FilterLogo}" -->
<Button
Grid.Column="1"
Content="Filter"
VerticalAlignment="Center"
HorizontalAlignment="Stretch">
</Button>
<ListBox
Grid.Row="1"
Grid.ColumnSpan="2"
ItemsSource="{Binding DataContext}"
SelectedItem="{Binding DataContext}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
I don't really know why my control is cropped to be honest. Any lead please ?
Short answer: it doesn't fit.
You have two columns in your main window. Since you have not specified which column to place the UserControl, it defaults to the first column. But your Width definitions force that column to be one fourth the size of the second column.
Your SearchBox simply doesn't fit in the space you have allotted to it.
If you remove the star (*) from this line:
<ColumnDefinition Width="8*" />
...it will fit without being cropped. (Alternatively, you could place the user control in the second column, or expand the main window, or shrink the width of your UserControl.)
When using stars in the column and row definitions, use them like percentages. Like this:
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".1*" /> <!-- 10% -->
<ColumnDefinition Width=".9*" /> <!-- 90% -->
</Grid.ColumnDefinitions>
This is what the XAML reader interpreted from your usage:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" /> <!-- 800% -->
<ColumnDefinition Width="*" /> <!-- 100% -->
</Grid.ColumnDefinitions>
This will have undesired results.

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>

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

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>

WPF. TextBlock and TextBox aligned properly

What I have when size of Border container is wide enough:
Name Value NameLonger Value
then size of Border gets smaller and I have something like this:
Name Value NameLonger V
I used WrapPanel and achieved something like this:
Name Value
NameLonger Value
It is better but I would like to achieve something like this:
Name Value
NameLonger Value
Is it possible to achieve such thing?
not sure if i totally understand what you are explaining but based on what i think yiou are describing, would this be what you are looking for?
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<!--Put your textblocks in here-->
</Grid>
</Border>
Use a grid as your container panel. Set the first column width to use as much width as it needs ("auto") and the second column to use the rest ("*").
Place the textblock in the first column, and the textbox in the second.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<textblock .../>
<textbox grid.column="1".../>
</Grid>

Resources