Size change event for the usercontrol

Docking/MDI for WPF Forum

Posted 12 years ago by keshav bansal
Version: 12.1.0561


I am able to register the usercontrol as  the toolwindow using the prism integration with the docking and it is linked to the other docksite  and i wrote the sizechange behaviour for the usercontrol that set the height and width for the usercontrol so as i run the application that will call the sizechanged behaviour for the usercontrol .but when i dock toolwindow with that usercontrol to the linked dockisite ,its not calling the sizechanged behaviour but it should call as per the WPF COMMAND ROUTING EVENT THROUGH BEHAVIOUR.

Comments (5)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Keshav,

We don't do anything that would prevent a SizeChanged event from firing.  If you set the Width and Height on the UserControl though, you have set a fixed size and thus even if its parent tool window changes size, the UserControl never will.  You should not set Width and Height on the UserControl if you want that event to be raised as the parent tool window changes size.

[Modified 12 years ago]

Actipro Software Support

Posted 12 years ago by keshav bansal


Suppose i have two usercontrols which are register as the two toolwindows toolwindow1 and toolwindow2 with their respective docksites docksite1 and the docksite2. these toolwindows are linked with the basedocksite.and i also created the sizechanged behaviour using the WPF COMMAND ROUTING EVENT THROUGH BEHAVIOUR for these usercontrols.when i run the application  they (usercontrols as the toolwindows)are displayed as vertically .So when i drag the tool window i am setting the height width for that dragged toolwindow using

DockSite.SetControlSizeForState(toolWindow.ParentContainer, newSize(230, 165), DockingWindowState.Docked);

and docked to the top of the base docksite,Then its not calling the OnsizeChanged behaviour for that toolwindow.

As i set the control size but this settted size is different than the original size so it should call as per the WPF COMMAND ROUTING EVENT THROUGH BEHAVIOUR.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Keshav,

DockSite.SetControlSizeForState only provides a hint for how a container should be positioned when it is docked into some other layout.  It doesn't cause an immediate size change or anything.  Only through the container changing real layout size will WPF fire the SizeChanged event.  WPF fires that event when it detects a real layout size change, not us.

Actipro Software Support

Posted 12 years ago by keshav bansal


As its calling only first time but not calling again size changing means again docking the toolwindow to otherdocksite that have usercontrol.

For the simple docking application code below.Main application window that consist of docksite which have toolwindow and toolwindow have usercontrol.As shown below View and View2 are the usercontrols and consist of only single button and the OnSizchanged  function.


<docking:DockSite x:Name="BaseDocksite2"><docking:Workspace>

<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition Height="50"/></Grid.RowDefinitions>

<docking:DockSite x:Name="docksite1" Grid.Row="0">


</docking:DockSite><docking:DockSite x:Name="docksite2" WindowDragging="docksite2_WindowDragging" Grid.Row="1">

<docking:ToolWindowContainer><docking:ToolWindow x:Name="Toowindow2"><Common:View2></Common:View2></docking:ToolWindow></docking:ToolWindowContainer>




As view and View2 are same like below

<UserControl x:Class="DockingApplication.View"xmlns=""xmlns:x=""xmlns:mc=""xmlns:d=""mc:Ignorable="d"xmlns:Common="clr-namespace:DockingApplication"Common:SizeChangedBehaviour.OnSizeChanged="{Binding UserControlSizeChanged}"><Grid><Button Content="View1" Background="Yellow"></Button></Grid></UserControl>

CodeBehind file for the above consist of tHE OnSizeChanged function whwnever the size of the usercontrol changes.

Size change Behaviour

public class SizeChangedBehaviour{

publicstaticDependencyProperty OnSizeChangedProperty = DependencyProperty.RegisterAttached("OnSizeChanged",typeof(ICommand),typeo(SizeChangedBehaviour),newUIPropertyMetadata(OnSizeChanged));

publicstaticICommand GetOnSizeChanged(DependencyObjecttarget){

return (ICommand)target.GetValue(OnSizeChangedProperty);}

publicstaticvoid SetOnSizeChanged(DependencyObject target, ICommandcommand){

target.SetValue(OnSizeChangedProperty, command);


privatestaticvoid OnSizeChanged(DependencyObject target, DependencyPropertyChangedEventArgse){

FrameworkElement ele = target asFrameworkElement;

if (ele != null){

ele.SizeChanged -= SizeChanged;

if (e.NewValue != null){

ele.SizeChanged += SizeChanged;}}}

staticvoid SizeChanged(object sender, SizeChangedEventArgse){

object[] sizeChangeParam = newobject[2];

var frameworkelement = sender asFrameworkElement;

var dpo = e.OriginalSource asDependencyObject;

if (frameworkelement == null || dpo == null)


UIElement uiElement = sender asUIElement;

ICommand command = (ICommand)uiElement.GetValue(OnSizeChangedProperty);sizeChangeParam[0] = sender;

sizeChangeParam[1] = e;



asFrameworkElement).SizeChanged -= SizeChanged;}


As the size changes or as i dock the toolwindow ,the command that is UserControlSizeChanged should call that corresponding OnSizeChanged function.


If i dock the toolwindow to the docksite more than one time then function OnSizeChanged  calls only first time not every time when user dock the toolwindow.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA

I'm sorry but what you've written seems very confusing and I'm not sure what you're trying to do.  Why don't you just attach to the UserControl.SizeChanged event directly?  That seems much easier.

Actipro Software Support

The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.