![Wpf: Ogni tanto penso che si siano fumati il cervello… header image](/img/posts/default-header.jpg)
Wpf: Ogni tanto penso che si siano fumati il cervello…
Provate a fare una Window ridimensionabile con il mouse (con il ResizeGrip per intenderci) ma ne massimizzabile ne minimizzabile… con Windows Forms era una banalità, con Wpf non si può :-o
WindowControlBoxBehavior
la soluzione, pezza secondo me, è questa:
un po’ di sano P/Invoke utilizzabile via xaml così:public sealed class WindowControlBoxBehavior : Behavior<Window> { public Boolean AllowMaximize { get; set; } public Boolean AllowMinimize { get; set; } RoutedEventHandler h; public WindowControlBoxBehavior() { h = ( s, e ) => { var isDesign = DesignTimeHelper.GetIsInDesignMode(); var hWnd = new WindowInteropHelper( this.AssociatedObject ).Handle; if( !isDesign && hWnd != IntPtr.Zero && ( !this.AllowMaximize || !this.AllowMinimize ) ) { var windowLong = NativeMethods.GetWindowLongPtr( hWnd, Constants.GWL_STYLE ); if( !this.AllowMaximize ) { windowLong = windowLong & ~Constants.WS_MAXIMIZEBOX; } if( !this.AllowMinimize ) { windowLong = windowLong & ~Constants.WS_MINIMIZEBOX; } NativeMethods.SetWindowLongPtr( hWnd, Constants.GWL_STYLE, windowLong ); } }; } protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.Loaded += h; } protected override void OnDetaching() { this.AssociatedObject.Loaded -= h; base.OnDetaching(); } }
producendo il risultato desiderato:<i:Interaction.Behaviors> <bhv:WindowControlBoxBehavior AllowMaximize="False" AllowMinimize="False" /> i:Interaction.Behaviors>
![image image](https://lh4.googleusercontent.com/-uzE-tcrVszQ/TzJygJjRygI/AAAAAAAABIo/QPQwneXK9pM/9b70f997-30b0-4669-9668-f39d75157bf9.png)
![image image](https://lh5.googleusercontent.com/-ja_aUEvJNu0/TzJyhmzYJ8I/AAAAAAAABI0/W2p7GlhQrvw/ecf1d948-e43c-4a2a-9fe9-d487114da266.png)
dove i bottoni per massimizzare e minimizzare non ci sono ma la finestra resta comunque ridimensionabile.
.m