wpf ScrollViewer and SizeChanged event - wpf

Can anyone explain to me the relationship between ScrollViewer and SizeChanged event? Whenever I put a scrollViewer around a grid, numerous SizeChanged event gets fired. What is the relationship between the two? Thanks a lot.
EDIT:
From mdm20's comment, I noticed that the ActualWidth and ActualHeight of the grid increases continuously if I wrap the grid around a ScrollViewer. Can anyone explain why this is the case? Do I need to have hard values for the width and height of the grid?
EDIT #2:
The resizing is done through code posted below. Thanks for looking into this
private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
textCanvas.Width = chartGrid.ActualWidth;
textCanvas.Height = chartGrid.ActualHeight;
legendCanvas.Children.Clear();
chartCanvas.Children.RemoveRange(1, chartCanvas.Children.Count - 1);
textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);
AddChart();
}
Corresponding XAML code is below:
<ScrollViewer Name="chartScrollViewer">
<Grid Margin="0" x:Name ="chartGrid" Grid.Column="1" Grid.Row="1" ClipToBounds="True" Background="Transparent" SizeChanged="chartGrid_SizeChanged">
<Canvas Margin="2" Name="textCanvas" ClipToBounds="True" Grid.Column="1" Grid.Row="1" Height="1200">
<Canvas Name="chartCanvas" ClipToBounds="True">
<Canvas Name="legendCanvas" Background="Transparent" />
</Canvas>
</Canvas>
</Grid>
</ScrollViewer>

You are getting into a loop. I think what is happening is that when you change the canvas size, it prompts the grid to do a layout pass, which causes the ScrollViewer to do a layout pass, which causes the grid to resize itself, which starts the cycle over again.

Related

WPF scrollviewer overview control

in my WPF application I use Scrollviewer to show a diagram.
The diagram can be pretty big and complex and usually you can see only a smart part of a diagram in the Scrollviewer control.
To facilitate the diagram navigation I will create a control which should contain the whole diagram picture. If you click on specific place in this control the Scrollviewer should scroll the diagram to this place.
Something like that
What is the easiest way to do that?
Many thanks and sorry for my terrible English.
XAML:
<Grid>
<ScrollViewer x:Name="TheScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Image x:Name="FullImage" Source="Test.jpg" Stretch="None"></Image>
</ScrollViewer>
<Border BorderBrush="Red" BorderThickness="1" VerticalAlignment="Bottom" HorizontalAlignment="Right">
<Image x:Name="ThumbnailImage" Height="100" Source="Test.jpg" MouseDown="ThumbnailImage_OnMouseDown"/>
</Border>
</Grid>
Code-behind:
private void ThumbnailImage_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var position = e.GetPosition(ThumnailImage);
var horOffset = position.X / ThumnailImage.ActualWidth * FullImage.ActualWidth;
var verOffset = position.Y / ThumnailImage.ActualHeight * FullImage.ActualHeight;
TheScrollViewer.ScrollToHorizontalOffset(horOffset);
TheScrollViewer.ScrollToVerticalOffset(verOffset);
}

WPF ScrollViewer resize problem

I've got a scrollviewer in a WPF window that is resizing according to its contents no matter what I do. This is kind of a problem since it will resize my window as well, and in my opinion defeats the purpose of a scrollviewer.
The window in question has its SizeToContent set to "WidthAndHeight" which I know can be set to "Manual" to resolve the scrollviewer issues, but then I have to go and fiddle with my window layout.
Is there any way to get the best of both worlds here? All I want is for the scrollviewer to stay the same (its current) size any time its content changes.
EDIT:
If you really need to see it.....
<Window SizeToContent="WidthAndHeight">
<Grid Width="200" Name="ThinkBeforeSpeaking">
<ScrollViewer VerticalScrollBars="Auto" HorizontalScrollBars="Auto">
<ContentControl Content={Binding AnythingYouCanImagine}"/>
</ScrollViewer>
</Grid>
</Window>
Doesn't need to be a content control inside of the viewer, just anything bigger than the window in either dimension.
I've just come across this problem myself, and I've found a suitable solution in wrapping the ScrollViewer in a Canvas, so that it doesn't take part in the measuring pass, and telling the ScrollViewer to use the ActualHeight and ActualWidth of the Canvas so that it takes up all the space the Canvas has been given:
<Window SizeToContent="WidthAndHeight">
<Grid Width="200" Name="ThinkBeforeSpeaking">
<Canvas>
<ScrollViewer VerticalScrollBars="Auto" HorizontalScrollBars="Auto"
Height="{Binding Parent.ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"
Width="{Binding Parent.ActualWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}">
<ContentControl Content={Binding AnythingYouCanImagine}"/>
</ScrollViewer>
</Canvas>
</Grid>
</Window>
As it stands, there's nothing telling the window to have any height at all, so either one of the existing controls (Window, Grid or Canvas) needs a defined Height/MinHeight, or other controls need to be added which will provide their own Height/MinHeight.
I'm assuming you want your Window to start sized large enough to fit it's content, but then if the content changes it will not resize.
If so, you'd need to reset the SizeToContent option when the Window is loaded, like so:
public MainWindow() {
InitializeComponent();
this.Loaded += (s, e) => { this.SizeToContent = SizeToContent.Manual; };
}
In your example you're using the WidthAndHeight behavior for your Window. The Window's Width will expand to the 200px you set on your Grid, but then you have no control to dictate which Height the Window needs to have.
Consequently, the Window's Height will resize to the ScrollViewer's content, unless you specify a fixed Height or MaxHeight for your ScrollViewer or Grid (Or any other container between the Window and the ScrollViewer).
<Window SizeToContent="WidthAndHeight">
<Grid Width="200" Height="150" Name="ThinkBeforeSpeaking">
<ScrollViewer VerticalScrollBars="Auto" HorizontalScrollBars="Auto">
<ContentControl Content={Binding AnythingYouCanImagine}"/>
</ScrollViewer>
</Grid>
</Window>
Think of it, which Height would the Window have otherwise? None? Infinite? There's nothing in your XAML that specifies it.

Silverlight MouseLeave showing inconistent behaviour

So, my problem is that the MouseLeave event only gets triggered if I move my cursor at a certain speed. Below is a Thumb with a Border. Click the thumb and keep the mouse button down. Move slowly outside the border = no event, Move fast outside the border = event.
<Grid x:Name="LayoutRoot" Background="White">
<Border BorderBrush="Black" BorderThickness="3" Width="200" Height="100"
MouseLeave="Border_MouseLeave">
<Thumb />
</Border>
</Grid>
private void Border_MouseLeave(object sender, MouseEventArgs e)
{
MessageBox.Show("Border_MouseLeave");
}
Is it a bug in silverlight or am I missing something?
Thanx
/Mike
Thanx for the anwers, here's an update:
It seems to have something to do with MouseCapture as Guy suggests. The reason being that I also get the problem using Button or a Rectangle that captures the mouse in code-behind.
If I put a Grid between the Thumb and the Border the problem disappears so I think I will go with that.
I also noticed a related problem as I played around some.
<Grid Width="200" Height="100" Background="Transparent"
MouseLeave="Border_MouseLeave">
<Button />
<Rectangle Width="40" Height="40" Fill="Violet"
HorizontalAlignment="Left" />
</Grid>
If I press the left button and move the cursor to the left over the Rectangle and out the MouseLeave event is not registered.
This is all very strange.

WPF: GroupBox dynamic height

I have a textbox and datagrid inside of a dockpanel that is in a WPF groupbox.
<GroupBox Margin="8,142.04,1.783,230.4" Height="Auto" Header="Desired Meeting Outcomes (decisions or actions)?" MaxWidth="635" MinWidth="550" FontWeight="Bold" FontSize="13.333" BorderBrush="#FFD5DFE5" MinHeight="106" VerticalContentAlignment="Stretch">
<DockPanel Margin="0">
<local:TextboxControl Margin="0" d:LayoutOverrides="Height, HorizontalMargin" Width="538.217" VerticalAlignment="Top" HorizontalAlignment="Left" DockPanel.Dock="Top"/>
<local: Height="Auto" HorizontalAlignment="Left" MinHeight="25" MinWidth="538" DockPanel.Dock="Top"/>
</DockPanel>
</GroupBox>
I am adding rows in the datagrid dynmaically from the textbox causing the datagrid to grow. However, my groupbox's height is not growing dynamically even though its height is set to Auto. How can I get my groupbox to grow and shrink based upon the size of the contents that it holds?
You have margins set on all 4 sides with a VerticalAlignment of Stretch. In a Grid this will basically give you a GroupBox that sizes with its parent but not its content. Remove the margin from the right and bottom and change the VerticalAlignment to Top.
The margins are the order of L, T, R, B. So zero out the last two. Height=Auto and VerticalContentAlignment=Stretch are the defaults so you can get rid of those too. Try to keep the XAML as clean as possible.
It's clear from the markup that you're using Blend or Visual Studio's designer. I would suggest using the designer for "preview" mode rather than editing. Although it's gotten much better I find the layout behavior of the designer in both products to be very frustrating. Getting familiar with creating XAML by hand pays dividends in the long run.
EXAMPLE
As per the comments, I'm adding an example of how you would have a DataGrid that causes its parent elements to automatically grow based on height. Notice that only the Window itself has a fixed size. For a Window, if you wanted to make it grow based on height you could set SizeToContent=Height. Notice how you only need to set VerticalAlignment=Top on the outermost element.
MainWindow.xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="Green" VerticalAlignment="Top">
<Border Margin="5" BorderBrush="Yellow" BorderThickness="4">
<GroupBox Header="Data Grid" Background="Orange">
<DataGrid x:Name="dg" AutoGenerateColumns="True" />
</GroupBox>
</Border>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow( )
{
InitializeComponent( );
var items = new ObservableCollection<DateTime>( );
dg.ItemsSource = items;
var timer = new DispatcherTimer( );
timer.Interval = TimeSpan.FromSeconds( 2 );
timer.Tick += ( s, e ) => items.Add( DateTime.Now );
timer.Start( );
}
}
What is the container of the GroupBox ? It could prevent it from growing.
For example, if the container is the Windows, does it have SizeToContent="Height" ?

Perfect Center on WPF Canvas

Since the canvas requires a Top/Left for placement, if you want to center something, is adding a grid at the proper Canvas.Top with HorizontalAlignment="Center" the best way to do it, or is there a better way?
This snip is a 150X300 canvas, with some content centered in a grid ....
<Canvas Width="150" Height="300">
<Grid Canvas.Top="75" Width="106" HorizontalAlignment="Center">
{whatever you want centered}
</Grid>
</Canvas>
Guy's solution works, but you may have to tweak z-order and visibility if you're juggling hit testing.
Another alternative is having the Grid inside the Canvas (as you've specified in your XAML) with the Height/Width set to (or bound to) the Height/Width of the Canvas. Then setting HorizontalAlignment/VerticalAlignment to Center for the contents of your Grid.
I'm not sure if this will meet your exact requirement, but if you put both the canvas and the content inside a grid as peers, it will get you a centered result:
<Grid>
<Canvas Width="150" Height="300"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="106" Content="Click"/>
</Grid>

Resources