Auto Sizing between grid columns - wpf

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>

Related

DataTemplate of Grid inside ListView is not resizing

I have a ListView that lays on top of UI graphics in my UserControl that I want to show and size using a Grid that matches the parent grid. I'm trying to template the way each item shown and sized using the ListView.ItemTemplate. The issue I'm having is that the DataTemplate is not sizing to the main Grid of the UI, and I'm not sure why.
This is what I've done, I think it a fairly simple and straight forward datatemplate:
<ListView ItemsSource="{Binding Path=StatusFilter}" Grid.Row="2" Grid.ColumnSpan="12">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="92"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding Path=ECNNumber}" Grid.Column="0"/>
<Label Content="{Binding Path=DateECNDue}" Grid.Column="1"/>
<Label Content="{Binding Path=Model.Model}" Grid.Column="2"/>
<--More stuff for each column-->
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When this is displayed the grid is completely bypassed and each element is shown like a horizontally aligned stackpanel. To test that my DataTemplate wasn't faulty I threw it into an ItemsControl and it worked perfectly. It resized with the parent grid without any issues.
Why isn't this working for the ListView, and how do I get it to work? I need the selection and scrollview capability of the ListView otherwise I'd be using the ItemsControl.
I read something about Grid.SharedSizeScope but that doesn't work with dynamic sizing of grid, and that's all I could really find on this issue.

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.

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>

Wpf controls overlay?

I have Dockpanel on which there are two buttons (left and right sides) and a scrollviewer at the bottom. Is it possible to hide the left and right sides of this scrollviewer UNDER this buttons?
You could use a Grid instead of a DockPanel, either use the alignments or create columns and adjust the ColumnSpan, example of the latter:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- Order matters, earlier controls are at the bottom unless you use Panel.ZIndex -->
<ScrollViewer Grid.Column="0" Grid.ColumnSpan="3"/>
<Button Grid.Column="0" Content="Left"/>
<Button Grid.Column="2" Content="Right"/>
</Grid>
(DockPanel is quite a poor control which can easily be replaced with a Grid in about every case)

XAML for Three way splitter in WPF

I want to split the main window in three way Grid Spliiter same alike outlook style.
and all the controls nested inside in the three window is resized using Grid Splitter.
I am trying to do that using defining Row Definition of Grid and placing each control in Row 1 or column 1..
Its nice to have , if any body has some kind of template to kick start with this kind of layout.. I don't want too complex sample..
This is a perfect resource - Build an Outlook 2007 UI Clone
You can download the full demo(OutlookUI HOL Basic - Final), I think it is what you are looking for
Edit(after comment)
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" Width="1" />
<GridSplitter Grid.Column="3" Width="1" />
</Grid>

Resources