creating standard behaviour custom maximise/restore button for custom window in WPF

StackOverflow https://stackoverflow.com/questions/22379162

  •  14-06-2023
  •  | 
  •  

Pregunta

I have used the example at http://www.youtube.com/watch?v=EuhhL_NF-B0 and downloaded the source code to form the basis of my custom window chrome. i have changed the look a fair bit because it's fairly ugly looking. Additionally I have added custom images for the close, maximise and minimise buttons.

However, I note that standard behaviour is for a different image to be displayed for maximise/restore based on whether the window is in a maximised or normal state at the time.

has anyone got a suggestion for how to do this?

¿Fue útil?

Solución

Just add both buttons and hide unnecessary with triggers. Two buttons is convenient to easy implement different behavior.

Add something like this to your template:

<ControlTemplate TargetType="{x:Type Window}">
    ...
    <cc:ImageButton ImageSource=".../CloseWindow.png" x:Name="closeButton" Click="OnCloseClick" />
    <cc:ImageButton ImageSource=".../MaximizeWindow.png" x:Name="maximizeButton" Visibility="Collapsed" Click="OnMaximizeClick" />
    <cc:ImageButton ImageSource=".../RestoreWindow.png" x:Name="restoreButton" Visibility="Collapsed" Click="OnRestoreClick" />
    <cc:ImageButton ImageSource=".../Help.png" x:Name="helpButton" Click="OnHelpClick" />
 ...
<ControlTemplate.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="ResizeMode" Value="CanResizeWithGrip" />
            <Condition Property="WindowState" Value="Normal" />
        </MultiTrigger.Conditions>
        <Setter TargetName="maximizeButton" Property="Visibility" Value="Visible" />
    </MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Never mind cc:ImageButton - just replace it with Image or any. Also You may omit the first trigger condition if You do not need to style not resizable windows.

It is also possible to change ImageSource for a single Image. But two buttons are more flexible.

If it is needed i can post full source of my window style. It works fine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top