0

I have a WPF application that is borderless so I needed to create my own buttons for windows functions and needed to add a MouseDown event to allow application to move when clicked and dragged. The document outline is:

Window grid canvas grid stackPanel - this is where my windows close max and min buttons are.

here is the code for the StackPanel:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Canvas.Left="689" Canvas.Top="5">
            <TextBlock x:Name="Minimize" Text="0" FontFamily="Webdings" Foreground="Black" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0" MouseLeftButtonUp="Minimize_MouseLeftButtonUp"/>
            <TextBlock x:Name="Maximize" Text="1" FontFamily="Webdings" Foreground="Black" Margin="5,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Right" MouseLeftButtonUp="Maximize_MouseLeftButtonUp"/>
            <TextBlock x:Name="Close" Text="r" FontFamily="Webdings" Foreground="Black" Margin="5,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Right" MouseLeftButtonUp="Close_MouseLeftButtonUp"/>

        </StackPanel>

When I add the MouseDown dragmove method, it cancels out the StackPanel and does not allow me to close the application as shown in the code above. I think the document outline needs a specific order? need some help with maybe understanding how it prioritizes the event handlers and if I need to rearrange my document outline.

Nicolas Dias
  • 641
  • 11
  • 22
DHB
  • 161
  • 3
  • 15
  • Personally I'd rather use the Click event, even if it means using a ` – Rachel Nov 24 '15 at 16:32
  • although I did start with buttons and it worked fine however the appearance of the buttons didn't really strike me as "good looking". Which is why I went with the textblock webdings look. (more modern). Referencing: http://hookscode.com/wpfborderlesswindowcontrols/ – DHB Nov 24 '15 at 17:06
  • WPF is meant to have "lookless" controls, so if you want the functionality of a Button, it's best to use a ` – Rachel Nov 24 '15 at 17:11
  • can you provide some code to help me maybe try what you're suggesting? I understand what you're saying but I was having issues with basically just the appearance with buttons. (This is my first wpf app that I am designing) – DHB Nov 24 '15 at 17:18
  • nvm figured it out, thanks for this! :) – DHB Nov 24 '15 at 17:30
  • Good to hear! I posted my answer anyways since I had just finished it, and it might help someone else with the same question :) – Rachel Nov 24 '15 at 17:34
  • definitely, I appreciate you thanks! – DHB Nov 24 '15 at 17:50

1 Answers1

1

Calling .DragMove prevents the MouseLeftButtonUp from executing on a TextBlock, however the .Click event of a Button should work correctly.

If I remember correctly, the MouseDown event of a Button does not bubble up the UI tree, so the MouseDown event of the parent control (in this case, a StackPanel) does not get executed.

Since you are looking for the functionality of a Button using a different UI, I would recommend just using a <Button> that has it's Template overwritten.

For example,

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <TextBlock Text="{TemplateBinding Button.Content}"
                           FontFamily="Webdings" Foreground="Black">
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

It can be used like this :

<Button x:Name="Minimize" Text="0" 
        Style="{DynamicResource MyButtonStyle}" 
        Click="Minimize_Click" ... />
Rachel
  • 130,264
  • 66
  • 304
  • 490