DHB
DHB

Reputation: 161

WPF mouse down and mouseleftbuttonup event conflicts. 1 deactivates the other. (Borderless application)

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.

Upvotes: 0

Views: 561

Answers (1)

Rachel
Rachel

Reputation: 132558

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" ... />

Upvotes: 1

Related Questions