How do you detect when ResizeMode and WindowStyle is changed on a wpf window?
-
Saturday, September 22, 2012 2:14 PM
Hello. I am working with wpf windows and making dependency properties that are dependent on those two peoperties, namely ResizeMode and WindowStyle. Specifically, the properties are the system window frame width, height, and caption height as applicable to the current window instance. Also, I am using a window that derives from a special helper class I made which has a WndProc method, so I am prepared to override WndProc.
Thank you.
All Replies
-
Saturday, September 22, 2012 5:41 PMModerator
Bind them to a property in your code-behind or ViewModel, then you can trigger stuff on their setters.
Or make dependency properties to bind them to. Then you can use the PropertyChanged event handler to do stuff.
Here is an example of the first suggestion:
http://code.msdn.microsoft.com/Saving-and-Binding-Window-4b99d493
For the second suggestion I mean similar to the first, but instead of an InotifyPropertyChanged, use DependencyProperties like this:
public partial class MainWindow : Window { public ResizeMode MyResizeMode { get { return (ResizeMode)GetValue(MyResizeModeProperty); } set { SetValue(MyResizeModeProperty, value); } } // Using a DependencyProperty as the backing store for MyResizeMode. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyResizeModeProperty = DependencyProperty.Register("MyResizeMode", typeof(ResizeMode), typeof(MainWindow), new UIPropertyMetadata(ResizeMode.NoResize, ResizeModeChanged)); static void ResizeModeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { //DO STUFF! } public MainWindow() { InitializeComponent(); DataContext = this; }Notice "DataContext = this"
Now you can bind directly from your Window XAML to "MyResizeMode", then when it changes, you can react in the PropertyChanged handler.
<Window x:Class="MyProject.MainWindow" ResizeMode="{Binding MyResizeMode}" ...>Regards,
Pete
#PEJL
- Edited by XAML guyMicrosoft Community Contributor, Moderator Saturday, September 22, 2012 5:42 PM typo
- Edited by XAML guyMicrosoft Community Contributor, Moderator Saturday, September 22, 2012 5:43 PM typo
- Edited by XAML guyMicrosoft Community Contributor, Moderator Saturday, September 22, 2012 5:43 PM typo
- Edited by XAML guyMicrosoft Community Contributor, Moderator Saturday, September 22, 2012 5:45 PM fixed propertymetadata default value
-
Saturday, September 22, 2012 6:22 PM
Try DependencyPropertyDescriptor. Event will be raised when the related property will change
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded Dim d1 As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(Window.ResizeModeProperty, GetType(Window)) d1.AddValueChanged(Me, New EventHandler(Sub() MessageBox.Show("Resize Mode Changed. New Mode=" & Me.ResizeMode.ToString()) End Sub)) Dim d2 As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(Window.WindowStyleProperty, GetType(Window)) d2.AddValueChanged(Me, New EventHandler(Sub() MessageBox.Show("Window Style changed. New Style = " & Me.WindowStyle.ToString()) End Sub)) End Sub
Gaurav Khanna | Microsoft VB.NET MVP
- Marked As Answer by Alex Kven Saturday, September 22, 2012 9:45 PM
-
Saturday, September 22, 2012 9:47 PM
This is the answer I am looking for. Thank you, I did not know about DependencyPropertyDescriptor. I new that DependencyProperties has their own way of reporting changes, but I didn't know I could subscribe to it directly, I thought it was only handled internally for data bindings.
Thank you!

