WPF Bottomless DataGrid keeps expanding downward - wpf

I've got this Grid which contains a TabControl with a number of TabItem.
Each TabItem loads a UserControl.
<Grid>
<TabControl>
<TabItem>
<local:UsersControl/>
</TabItem>
</TabControl>
</Grid>
In this specific UserControl I placed a Grid with nested a DockPanel, left side for menu, right for content.
On the right side are loaded a Border containing a single UserControl relative to the selection on the menu on the left.
<Grid>
<DockPanel>
<Border>
<UserControl Content="{Binding MainDock, UpdateSourceTrigger=PropertyChanged}"/>
</Border>
</DockPanel>
</Grid>
Finally, the UserControl consists of a StackPanel which contains a TextBlock and a ScrollViewver with inside a DataGrid.
<StackPanel>
<TextBlock/>
<ScrollViewer>
<DataGrid>
<!-- datagrid stuff -->
</DataGrid>
</ScrollViewer>
</StackPanel>
The problem consist in the latest StackPanel being bottomless and expanding well over the bottom of the window application, which never activate the ScrollViewer hiding part of the grid data, when there are enough rows.
Any idea how to solve this?

Related

Scroll position within a ScrollViewer?

I have a form that with a TabControl that contains a number of TabItems. Each of the TabItems contains a ScrollViewer that contains some various contained content.
My problem - if the size of the form and the TabControl and the contained content is such that the ScrollViewer displays its vertical scrollbars, the content is displayed vertically centered within the ScrollViewer. In order to see the top of the content, the user needs to manually scroll to the top.
Oddly enough, when the size is such that the Horizontal scrollbars are displayed, the content is initially aligned to the left, which is what I want.
How do I make these ScrollViewers open with the scrollposition initialized to the top?
<ParentUserControl>
<Grid>
<TabControl>
<TabItem>
<ScrollViewer>
<Grid>
// Assorted Junk
</Grid>
</ScrollViewer>
</TabItem>
<TabItem>
<ScrollViewer>
<ChildUserControl />
</ScrollViewer>
</TabItem>
<TabItem>
<ScrollViewer>
<OtherChildUserControl />
</ScrollViewer>
</TabItem>
</TabControl>
</Grid>
</ParentUserControl>
I think what's happening is the ScrollIntoView event is being fired. This solution worked for me in the past: Stop WPF ScrollViewer automatically scrolling to perceived content

How to make controls resize in WPF equivalently to Windows Form's anchor/dock properties?

I've read so many solutions to this problem. Every one of them fails to solve my problem. No matter what container I put the control into or what properties I set on the control/container it will not budge.
I have a scroll viewer with a control within. I want it to resize with the window when the user resizes it at runtime. All I need is anchor=top, bottom, left, right. I don't understand why this is so elusive in WPF and why container objects and all kinds of property assignments need to be involved to accomplish what a single property can in Windows Forms. But every solution to this problem still results in my control staying at exactly its design time size as the window is resized at runtime. What's the simple way to get a grip on dynamic control sizing in WPF?
This has caused me grief as well and AlexK helped me see the light. The trick is NOT to set the Height and Width.... Set these to AUTO and use the MARGIN to set the size. Set the HORIZONTALALIGNMENT and VERTICALALIGNMENT to STRETCH and then the anchor functionality works.
The control needs to stretch, that's all there should be to it:
<MyControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
Stretch replaces setting anchors to both respective sides.
For help on panels see this overview. Also see the documentation of the layout system.
Most controls automatically stretch, if you have a DataGrid it should stretch too, this example contains a DataGrid and a TextBlock which shows its size:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="grid">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Tag}" Header="Occupation"/>
</DataGrid.Columns>
<FrameworkElement Name="Skeet" Tag="Programmer"/>
<FrameworkElement Name="Gravell" Tag="Programmer"/>
<FrameworkElement Name="Steve" Tag="Coffee Getter"/>
</DataGrid>
<TextBlock Grid.Row="1">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding ElementName="grid" Path="ActualWidth"/>
<Binding ElementName="grid" Path="ActualHeight"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Window>
If you size the window down the DataGrid's ScrollBars should appear.
I assume the control that does not resize is your custom control.
Use DockPanel as a container.
Remove explicit Width and Height properties from your control.
If you work in VS2008, then this causes inconvenience, because you control would collapse to the minimal size when viewed in the designer.
Expressions Blend and starting from VS2010 both respect designer namespace, so you can specify design time only control size.
For that add the following to your control:
<UserControl ...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignWidth="WWW" d:DesignHeight="HHH">
....
</UserControl>
d:DesignWidth and d:DesignHeight specify the design time width and height.
This question is old but, since I found my here I thought I would throw out my solution. The code below is for a tab control with two tabs and each tab contains a control to fill the tab. I removed the explicitly set width and height, and set the horizontal and vertical alignments to auto on all the controls I wanted to resize. The tab control stretches wit the main window. The controls in the tabs stretch to fill the tabs. The information came from the answers here. I just put up a complete example.
Hope this is useful to someone.
<TabControl HorizontalAlignment="Stretch"
Margin="91,0,0,0" Name="tabControl1"
VerticalAlignment="Stretch" >
<TabItem Header="DataGrid" Name="tabItem1">
<Grid>
<DataGrid AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
Name="dgFTPLog"
VerticalAlignment="Stretch"
Margin="6,6,0,0" />
</Grid>
</TabItem>
<TabItem Header="Log" Name="tabItem2">
<Grid>
<TextBox
HorizontalAlignment="Stretch"
Margin="6,6,0,0" Name="txtLog"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
TextChanged="txtLog_TextChanged" />
</Grid>
</TabItem>
</TabControl>
I've came across this issues as well.
and Attempted at binding to parent controls ActualHeight and ActualWidth properties except this only works if you do it via code behind not by XAML.
i.e
XAML
<MyParentControl x:name="parentControl" SizeChanged="parentControl_SizeChanged">
<ChildControl x:name=childControl" />
</MyParentControl>
in the .cs code behind
private void parentControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
childControl.Height = parentControl.ActualHeight;
childControl.Width = parentControl.ActualWidth;
}

Prevent WPF control from expanding beyond viewable area

I have an ItemsControl in my user control with a scroll viewer around it for when it gets too big (Too big being content is larger than the viewable area of the UserControl). The problem is that the grid that it is all in just keeps expanding so that the scroll viewer never kicks in (unless I specify an exact height for the grid). See code below and thanks in advance.
<UserControl x:Class="BusinessObjectCreationWizard.View.TableSelectionPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<GroupBox FontWeight="Bold" Height="300px"
Header="Tables"
Padding="2">
<ScrollViewer>
<ItemsControl FontWeight="Normal"
ItemsSource="{Binding Path=AvailableTables}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=DisplayName}"
IsChecked="{Binding Path=IsSelected}"
Margin="2,3.5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</UserControl>
This user control is loaded here
<Border Background="White" Grid.Column="1" Grid.Row="0">
<HeaderedContentControl Content="{Binding Path=CurrentPage}"
Header="{Binding Path=CurrentPage.DisplayName}" />
</Border>
I would like to not specify the height.
If you remove the Height from your GroupBox (which, as far as I understand, is what you want to do), then it will fill its container, unless there's a panel upstream that imposes its own sizing rules.
I used this simplified version of your XAML. I removed the template and the binding, and hard-coded some items, to make this stand alone; those changes won't affect the way layout is done.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<GroupBox FontWeight="Bold" Header="Tables" Padding="2">
<ScrollViewer>
<ItemsControl FontWeight="Normal">
<TextBlock>Foo</TextBlock>
<TextBlock>Bar</TextBlock>
<TextBlock>Baz</TextBlock>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</Window>
Run it, and you'll see that the content does indeed size to fit the window, and the scrollbar only enables when the window gets too small to see all three items. I believe this is what you want.
So the problem is most likely one of the parent panels, one you're not showing in your sample XAML. The problem you describe could occur if your GroupBox appears inside a StackPanel:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<StackPanel>
<GroupBox FontWeight="Bold" Header="Tables" Padding="2">
<ScrollViewer>
<ItemsControl FontWeight="Normal">
<TextBlock>Foo</TextBlock>
<TextBlock>Bar</TextBlock>
<TextBlock>Baz</TextBlock>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</StackPanel>
</Window>
Now the GroupBox appears at the top of the Window, sized to exactly fit its contents. If you shrink the Window enough, the GroupBox will be cut off -- because it's sized to fit its content, not its container. This sounds like the problem you're describing.
The reason is that StackPanel asks its children what their ideal height is (based on their content), and uses that height. Without StackPanel (or something similar), the default is to respect the control's VerticalAlignment, and if that's set to the default value of Stretch, then the control is stretched to fill its parent. This means it won't be taller than its parent, which sounds like what you want.
Solution: remove the StackPanel (or whatever else is causing you problems) and use something else. Depending on what you're trying to accomplish, you might have better luck with a DockPanel or a Grid. Hard to tell without knowing more about your layout.
Edit: Okay, it looks like the problem is indeed the HeaderedContentControl parent -- but not directly. HeaderedContentControl isn't a panel, so it doesn't do any layout of its own (and its descendant, GroupBox, doesn't have this same problem). The problem is its default template -- which includes a StackPanel. The good news is, you're free to use a different template, let's say one with a DockPanel instead:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<HeaderedContentControl>
<HeaderedContentControl.Style>
<Style TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<DockPanel>
<ContentPresenter ContentSource="Header" DockPanel.Dock="Top"/>
<ContentPresenter/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</HeaderedContentControl.Style>
<GroupBox FontWeight="Bold" Header="Tables" Padding="2">
<ScrollViewer>
<ItemsControl FontWeight="Normal">
<TextBlock>Foo</TextBlock>
<TextBlock>Bar</TextBlock>
<TextBlock>Baz</TextBlock>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</HeaderedContentControl>
</Window>
If you leave off the <HeaderedContentControl.Style> part, this reproduces your problem; but with the style in place, it allows the GroupBox to fill its container, so the ScrollViewer will get a scrollbar when you want it to.
If the previous answer doesn't fix the problem, you could also try binding the Width, Height of your grid to the ActualWidth, ActualHeight of your parent UserControl. Something like:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication.UserControl1"
x:Name="UserControl">
<Grid Height="{Binding ElementName=UserControl, Path=ActualHeight}"
Width="{Binding ElementName=UserControl, Path=ActualWidth}" />
In this case you aren't setting an explicit width and height but you are limiting the Grids width/height to the constraints of the UserControl it sits in.
I had the same issue, after reading this response I replaced all StackPanels with Grids in UserControl. It resolved the Scrollbar issue.
Try removing the grid entirely and setting the HorizontalAlignment and VerticalAlignment directly on the GroupBox. If a layoutpanel has only one child, it's often redundant... this migth be true in your case.
If that doesn't work... what's the parent of your grid control?
Why not just use a listbox instead of an itemscontrol, that has a built in scrollviewer.
They are different. If you do not want to have the items selectable, then don't use a ListBox. It is going to be heavier, and will also have the deselect a selection everytime the user clicks on an entry. Just put the ItemsControl in a ScrollViewer
I had the same problema with ListBox, it wasn't expanding and the scroll viewer didn't appear. I solved it as follows:
<UserControl x:Class="TesteView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid MaxHeight="710">
....
....
<StackPanel>
<ListBox MaxHeight="515"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Path=Teste,Mode=TwoWay}">
....
....
</ListBox>
</StackPanel>
</Grid>
</UserControl>

WindowsFormsHost ZOrder

It appears WindowsFormsHost control is set to display on top. Is there any way to change its z-order to allow other WPF controls on the same window to be visible on top of the WindowsFormsHost control?
Unfortunately no, because of the way the winformshost is composited into a WPF window it must appear on top.
See the z-order paragraph from here.
In a WPF user interface, you can change the z-order of elements to
control overlapping behavior. A hosted Windows Forms control is drawn
in a separate HWND, so it is always drawn on top of WPF elements.
A hosted Windows Forms control is also drawn on top of any Adorner
elements.
You can do a little trick. When you declare an WindowsFormsHost, it's parent is first HWND component. Usually it's root window. So, clip area for controls is whole window. I'll show an example with WPF ScrollViewer.
<Window>
<Grid>
<ScrollViewer Margin="20,50">
<ItemsControl ItemsSource="{StaticResource StringArray}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WindowsFormsHost>
<wf:Button />
</WindowsFormsHost>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Window>
In this case Buttons will be out of ScrollViewer bounds. But there's a way to create "intermediate" HWND item to clip WinForms area over ScrollViewer. Just place another WindowsFormsHost with ElementHost like below:
<Grid>
<WindowsFormsHost Margin="20,50">
<ElementHost x:Name="This is a clip container">
<ScrollViewer>
<ItemsControl ItemsSource="{StaticResource StringArray}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WindowsFormsHost>
<wf:Button />
</WindowsFormsHost>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</ElementHost>
</WindowsFormsHost>
</Grid>
Now clip area for Buttons is ElementHost and WinForms Buttons will be clipped by it on scrolling. Also you can create ControlTemplate for ContentContol and reuse it where you need it.
<ControlTemplate x:Key="ClipContainer" TargetType="{x:Type ContentControl}">
<WindowsFormsHost>
<ElementHost>
<ContentPresenter />
</ElementHost>
</WindowsFormsHost>
</ControlTemplate>
<Grid>
<ContentControl Template="{StaticResource ClipContainer}" Margin="20,50">
<ScrollViewer>
<ItemsControl ItemsSource="{StaticResource StringArray}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WindowsFormsHost>
<wf:Button />
</WindowsFormsHost>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</ContentControl>
</Grid>

WPF UserControl TreeView Height

I have a user control which contains a StackPanel and TreeView.
All controls have the Height="Auto"
When I use the Custom control on a window and set Height, say Height=800
The Stack Panel grows to this height, But the TreeView does not auto height adjust.
UserControl:
<UserControl x:Class="WPFDataBinding.ucCompanyTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="200" Width="300" Loaded="UserControl_Loaded">
<StackPanel>
<TextBlock Background="#505050" Foreground="Gold">Companys</TextBlock>
<TreeView Name="myTreeView" ItemTemplate="{StaticResource DetailTemplate}">
</TreeView>
</StackPanel>
Window1.xaml:
<StackPanel Orientation="Horizontal">
<local:ucCompanyTreeView Width="400" Height="600">
</local:ucCompanyTreeView>
</StackPanel>
The height of the stackpanel inside the usercontrol grows, but the Tree view does not.
I have tried placing the tree view in a grid, same
Setting Height="Auto" everywhere, same
Setting VerticalAlignment="Strech" everywhere, same
The Treeview was data bound, so I thought it was auto sizing after the data was bound, but removing this data binding same results.
I can do it through sizing events.... but I have had this issue before and just want to understand the logic behind height inheritance of "some" controls.
If you were to replace the StackPanel in the User Control with a DockPanel, the TreeView would fill the DockPanel by default...
<DockPanel>
<TextBlock DockPanel.Dock="Top" Background="#505050" Foreground="Gold">Companys</TextBlock>
<TreeView Name="myTreeView" ItemTemplate="{StaticResource DetailTemplate}">
</TreeView>
</DockPanel>
How to: Choose Between StackPanel and DockPanel

Resources