WPF UserControl fill ContentControl with fixed width and height - wpf

I have UserControls that I want to display in a ContentControl. So what I did(this is just one UserControl I have more):
<UserControl.Resources>
<DataTemplate x:Key="Suburb" DataType="{x:Type local:ViewSuburb}">
<local:ViewSuburb Width="Auto" Height="Auto" />
</DataTemplate>
</UserControl.Resources>
<ContentControl Grid.Column="1" Grid.Row="1" ContentTemplate="{StaticResource Suburb}">
</ContentControl>
(The ViewSuburb Width="Auto" Height="Auto" has no effect)
My problem is that the ViewSuburb is only used as a popup dialog thus in its .xaml I gave it a fixed width and height. If I set the width and height to auto then it is fine in the ContentControl and stretches accordingly but it stretches the popup over the screen. Since I gave it the fixed width and height the popup is fine but in the ContentControl it also has the fixed width and height.
Is there a way I can override the width and height of the SuburbView or if I set it to auto that the popup wouldn't stretch across the screen?
Thanks.

You should set the Width/Height of the Popup/Window that is the parent of the ContentControl.

Related

StackPanel interferes with sizing by margin

I have an app with a TabControl. On each TabItem is a DataGrid. The width and height are not set, the sizing is controlled by the margin so it sizes along with the tab.
<TabItem Name="tbRails" Header="Rails">
<DataGrid x:Name="dgRails" Margin="5,30,5,5" ItemSource=...
This is all working fine until I needed to put a CheckBox on one of the tabs. We can only have one child on a TabItem so I added a StackPanel and put in the CheckBox and the DataGrid.
<TabItem Name="tbRails" Header="Rails">
<StackPanel Name="pnlRails" Margin="10">
<CheckBox Name="chkCollapseItems" Content="Collapse Items" Margin="15" Checked="chkCollapseItems_Checked" ... />
<DataGrid x:Name="dgRails" Margin="5,30,5,5" ItemSource=...
After doing this the data grid has no scrollbar and doesn't respond to mouse wheel. I can click on the cell and it gets selected and I can arrow key down until it disappears out of the bottom. The width sizes to the window just like it did before but it appears to be sizing its height to fit the content (about 2600 rows).
Has anyone seen this before and how do we fix it? I can set the height of the grid and it works just fine but it no longer sizes itself to match the parent.
Use a Grid as panel instead of a StackPanel. A stack panel will measure its children with positive infinity, which does not restrict their height. In other words, the DataGrid will be scaled to display all its records and therefore there will be no scroll viewer.
Using a Grid with the RowDefinitions below, the CheckBox will size to fit its content and the DataGrid will get the rest of the available space in the TabItem.When this available space is not enough to display all records, it will automatically display a scrollbar.
<TabItem Name="tbRails" Header="Rails">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Name="chkCollapseItems" Content="Collapse Items" Margin="15" Checked="chkCollapseItems_Checked" ... />
<DataGrid Grid.Row="1" x:Name="dgRails" Margin="5,30,5,5" ItemSource= ... />
</Grid>
</TabItem>

How do you control the height of a scrollviewer inside a tabitem?

I am having trouble getting the height of a scrollviewer to adjust to whatever the height is of a tabitem.
XAML:
<TabItem header="Item">
<ScrollViewer Height={Binding ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=TabItem}}>
<Grid>
.
.
.
</Grid>
</ScrollViewer>
</TabItem>
When I do this the scrollviewer is only about 50 pixels high. The content inside displays fine. The viewing area height is limited.
I tried this.
<TabItem header="Item" x:Name="itemname">
<ScrollViewer Height="{Binding ElementName="itemname", Path=ViewportHeight}">
The scrollviewer is not limited but when the content inside the
scrollviewer grows it goes beyond the viewing area and the scrollbar
deos not work.
I have used the first (FindAncestor) example with a StackPanel and it works. Not sure why it won't with a TabItem.
Why not use the HorizontalAlignment / VerticalAlignment properties?
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretech">

Minimum resizing

Inside a grid have a ContentControl inside a Grid where I load a UserControl.
I want the user to resize the window, but how can I prevent resizing-down the window so it will be less the the user control?
In other words, user control should be always visible on the window.
<Grid>
<Border>
<ContentControl Content="{Binding Path=THeModel}">
</ContentControl>
</Border>
</Grid>
Use the MinWidth and MinHeight properties of the Window to set a minimum width and height.
<Window MinWidth="200" MinHeight="200" ... > ... </Window>
If it depends on its content, you can try binding these properties to the ActualWidth/ActualHeight of another control:
<Window MinWidth="{Binding ElementName=MyControl, Path=ActualWidth}" ... > ... </Window>
But this will only work well if MyControl has a fixed size - if it grows with the window, then the results will not be ideal.

WPF/Silverlight XAML Stretch Text Size to Fit container?

I've just started to play with WPF.
Is it possible to have the size of the text of a Label or TextBlock size itself to fill it's parent container?
Thanks,
Mark
You can use a ViewBox to visually zoom something to fit within its container. The other solutions here work, but they only stretch the control, not its content. The ViewBox will stretch both.
<!-- Big grid, will stretch its children to fill itself -->
<Grid Width="1000" Height="1000">
<!-- The button is stretched, but its text remains teeny tiny -->
<Button>
<!-- The viewbox will stretch its content
to fit the final size of the button -->
<Viewbox
Margin="4"
VerticalAlignment="Stretch"
Height="Auto">
<!-- The textblock and its contents are
stretched to fill its parent -->
<TextBlock
Text="Bartenders" />
</Viewbox>
</Button>
</Grid>
Depends on the parent container
Grid, DockPanel will stretch your control
StackPanel, WrapPanel will leave it to the control to size itself..
Set HorizonalAlignment/VerticalAlignment to "stretch".
Use DockPanel as parent container
<DockPanel>
<TextBlock />
</DockPanel>

WPF: Trigger a content resize with GridSplitter

I'm trying to force a grid/expander to reevaluate whether it needs a scrollbar, as it's showing emptiness.
I'm using this layout:
<Grid>
<toolstrip /> <!-- fixed height row -->
<Scrollviewer> <!-- * height -->
<Grid> <!-- all rows are 'Auto' height -->
<Expander />
<Expander> <!-- this one stretches far too high -->
<WPF Toolkit: DataGrid />
<Expander>
<GridSplitter/>
<Expander />
<Grid>
</Scrollviewer>
<stackpanel /> <!-- fixed height row -->
<Grid>
The DataGrid (WPF Toolkit) is bound to a property when the Window is initialized. Through some investigating, I've realized that when the window is initialized, the columns in the GridView start at about 10 pixels wide, then the content is added, then they're resized based on the sizes in the XAML (All using star widths - eg: 2*). This causes the grid to resize to about 6 times the height it needs to be as the window is showing, then it doesn't spring back and the only way to see what's at the bottom of the window is to either scroll or move the GridSplitter back up to where it should be and resize the Window. I haven't set the VerticalAlignment properties on anything.
So far I've tried all of the following called InvalidateArrange(), InvalidateVisual();, InvalidateMeasure() and UpdateLayout() on the problem expander and InvalidateArrange(), InvalidateScrollInfo(), InvalidateVisual() and UpdateLayout() on the Grid above it but it won't shrink back.
Is there any way I can force it to short of forcing the width of the columns in the DataGrid?
Try setting these properties on the ScrollViewer:
<ScrollViewer CanContentScroll="True"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
... content ...
</ScrollViewer>
If that doesn't work, can you provide a more exact representation of your XAML. Also, taking a look at the ScrollViewer in depth may help.

Resources