Reputation: 7306
I've been stuck with a project to be done in WPF, and the only experience that I have in WPF is what I've garnered from the internet (mostly SO) over the past week since I begin. So if my question seems elementary or the answer seems obvious to even a dim-wit, then I can only apologize and beg your patience!
Here is the XAML that constitutues my main window:
<Window>
<Grid Width="Auto" Height="Auto">
<StackPanel>
<DockPanel Margin="2">
<StackPanel Orientation="Horizontal">
<Border Name="leftPane" Height="Auto" CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" Padding="8">
<!-- Various controls here for the "left pane" -->
</Border>
<Separator BorderThickness="1" Margin="2"></Separator>
<Border Name="rightPane" Height="Auto" CornerRadius="6" BorderBrush="Gray" BorderThickness="2" Padding="0" Background="#FFF0F0F0">
<Canvas Name="canvasMain" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto">
<!-- I need to resize the rightPane and the canvasMain when the user manually resizes the main window -->
</Canvas>
</Border>
</StackPanel>
</DockPanel>
</StackPanel>
</Grid>
</Window>
The window more-or-less looks like this:
=================================================================
- -
- ------------- -------------------------------------------- -
- | Left Pane | | Right Pane | -
- | width:100 | | Need to expand width and height when | -
- | | | ...the main window is resized by the | -
- | | | ...user | -
- | | | | -
- | | | | -
- | | | | -
- | | | | -
- ------------- -------------------------------------------- -
=================================================================
My first inclination is to simply implement some logic in the SizeChanged event of the main window. But after a quick bit of googling I ran into topics about Attached Behaviors and Property Triggers and Event Triggers, which in turn got me thinking that perhaps the SizeChanged event isn't the best route to follow.
So, in a nutshell, what is the correct manner to dynamically resize the contents of a window?
Upvotes: 1
Views: 3557
Reputation: 44048
You're using the wrong containers, that's why your UI does not auto-size.
Remove the Grid
(because it's redundant) and the StackPanel
(because it's not an appropiate layout container for what you need here.)
<Window>
<DockPanel Margin="2">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left" Width="100">
<!-- Various controls here for the "left pane" -->
</StackPanel>
<Border>
<Canvas/>
</Border>
</DockPanel>
</Window>
You never handle the SizeChanged event for anything in WPF. Actually, most of the event-handling that you might be used to in other frameworks is rendered useless and unneeded in WPF due to it's advanced DataBinding capabilities and native support for resolution independence.
Upvotes: 3