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">
Related
I'm trying to place a Border around a StackPanel like so:
<Border CornerRadius="5" BorderBrush="#009999" Background="Transparent"
BorderThickness="2" Grid.Column="2" Grid.Row="0">
<StackPanel HorizontalAlignment="Center" Margin="0,30,0,0">
<!--Contents-->
</StackPanel>
</Border>
For some reason, the border is appearing around the column that the StackPanel is in, not the StackPanel itself. I can change the margin of the border, but that doesn't hold up if the screen is resized. I also tried getting rid of the StackPanel margin but that made no difference. The StackPanel is the only object in this column.
Border has default HorizontalAlignment equal to Stretch, so it takes full possible width.
Then StackPanel is centered inside the border because it has HorizontalAlignment="Center".
set HorizontalAlignment="Center" on Border. It will get width, equal to StackPanel width (plus Margin), and then will be centered
I think it is because you're setting the HorizontalAlignment to Center. Doing this will make the StackPanel size to whatever its content is.
If you move the Border to stackpanel your problems will be fixed :)
here it is:
<Grid>
<StackPanel Width="350" Height="200">
<Border BorderThickness="2" BorderBrush="Black" Width="350" Height="200">
<TextBlock>SomeText</TextBlock>
</Border>
</StackPanel>
</Grid>
I have a more StackPanels in my XAML. Every StackPanel has a border inside.
When I modify the Main Window the width follows the resizing. But the height follows only in one the bigger direction. If I make the Window smaller the height of the borders doesn't follows. So the effect is the Botton border line isn't visible. How can I do this ?
<Window x:Class="MyStackPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyCombobox" Height="356" Width="475">
<Grid>
<StackPanel x:Name="STP"
Margin="10">
<Border x:Name="STPB"
BorderBrush="#FFE80707"
BorderThickness="5"
CornerRadius="10"
Height="{Binding ElementName=STP,Path=ActualHeight}"/>
</StackPanel>
</Grid>
</Window>
!border normaly Looks like
Use a Grid instead of a StackPanel then the border will stretch to the
Grids height and width on resizing
<Grid x:Name="STP"
Margin="10">
<Border x:Name="STPB"
BorderBrush="#FFE80707"
BorderThickness="5"
CornerRadius="10" />
</Grid>
Window after your changes, Looks like the same after reize (make smaller) without your changes.
http://i.stack.imgur.com/SplMn.jpg
I'm fighting with a ScrollViewer inside a StackPanel. The ScrollViewer only shows one scrollbar depending on the StackPanel's orientation, what I kind of understand as the StackPanel thinks to be unlimited in that direction. I therefore tried to limit the size of the ScrollViewer by binding it to the StackPanel's width and height. When the application is brought up it shows both scrollbars but they do not resize properly. What is wrong or how should I do it.
(Remark: I know I can use a Grid instead of the StackPanel and the ScrollViewer behaves as expected. However once I place that Grid into a StackPanel the problem shows up again.)
<Window x:Class="tt_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<StackPanel x:Name="sp" Orientation="Horizontal">
<ScrollViewer
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
Width="{Binding ElementName=sp, Path=ActualWidth}"
Height="{Binding ElementName=sp, Path=ActualHeight}">
<Button Background="LightCoral" Width="500" Height="500">Hey</Button>
</ScrollViewer>
</StackPanel>
A Scroll viewer is useless inside a vertical StackPanel. A vertical StackPanel has its height set to infinity, so the ScrollViewer has all the size it wants and will never show the scrollbar. You should switch to a Grid or DockPanel.
I had the same problem, i solved it by switching stackpanel and scrollviewer.
I want to make it so that my TabControl is vertically scrollable, but I can't seem to do it. The following sample acts as though there was no scrollviewer at all. I even tried putting the TabControl inside the scrollviewer, or putting it all in a grid and constraining the height of the grid, but nothing works.
<DataTemplate x:Key="tabControlTemplate">
<TabControl ItemsSource="{Binding guiItems}" DisplayMemberPath="Title" Height="Auto" Template="{StaticResource mainTabControlTemplateEx}">
<TabControl.ContentTemplate>
<DataTemplate>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<StackPanel Margin="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl ItemsSource="{Binding guiItems }" ItemTemplateSelector="{DynamicResource templateSelector}"/>
</StackPanel>
</ScrollViewer>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</DataTemplate>
The actual problem is not clear from the question.
You are not getting to see scrollviewer and the content inside is clipped? PLease confirm if that is the case.
If the problem is you are getting to see entire content taking up all the available space, and you would like to control that using scroll viewer, then you would need to set 'MaxHeight' property on Scroll Viewer. This would limit the height of your DataTemplate and would make verticall scroll bar visible if the inner content goes beyond the MaxHeight.
Hope that helps.
In my application, I have an InkCanvas with an image in the background. When I draw on the InkCanvas to the bottom or left edge, the InkCanvas changes size to fit the sketch, which messes up the rendering of the image in the background. How do I stop the InkCanvas from resizing when strokes are applied outside its current size?
Current XAML:
<InkCanvas x:Name="DrawingArea"
Width="Auto"
Height="Auto"
ClipToBounds="True"
Background="{x:Null}" />
Background is set in code-behind
Without specifying a specific size, I found that binding the MaxWidth and MaxHeight to the ActualWidth and ActualHeight of the parent element keeps the InkCanvas from expanding outside the element.
<Grid x:Name="LayoutRoot">
<InkCanvas x:Name="DrawingArea"
MaxWidth="{Binding ActualWidth, ElementName=LayoutRoot}"
MaxHeight="{Binding ActualHeight, ElementName=LayoutRoot}"
Background="{x:Null}" />
...
</Grid>
You could hardcode the widths instead of using auto:
Width = "200"
Height = "200"