WPF Popup control on ImageMouseLeftButtonDown disappears immediately - wpf

I have a popup defined as follows,
<Popup x:Name="popLines"
Placement="Bottom"
IsOpen="False"
Width="145" Height="42"
StaysOpen="False"
PopupAnimation="Fade"
AllowsTransparency="True"
HorizontalOffset="-2" VerticalOffset="0">
<Grid Margin="2">
<Path StrokeThickness="0.7" StrokeLineJoin="Round" Fill="#FFFFFFFF" Stretch="Fill" Stroke="Black" Data="M6.5,0.5 L30.167,0.5 30.167,8.4999992 190.16701,8.4999992 190.16701,44.166001 0.5,44.166001 0.5,8.4999992 6.5,8.4999992 6.5,0.5 z">
</Path>
<Grid>
<StackPanel Orientation="Horizontal">
<TextBox BorderBrush="Black" BorderThickness="0.5" Margin="5,10,2,2" Width="110" Height="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ToolTip="Excel File Path"></TextBox>
<Image Source="/App_Desktop;component/Resources/save.png" Margin="2,10,5,2" Width="16" Height="16"></Image>
</StackPanel>
</Grid>
</Grid>
</Popup>
I set IsOpen=true when an image MouseLeftButtonDown event fires.Except, the popup disappears as soon as it appears. What is going wrong?

I think what happens is, that the the MouseLeftButtonUp event fires while the Mouse is not over the Popup. Try holding the mousebutton down and move your mouse so it is above the popup to see if it stays open, and then release the mousebutton.
you can solve this by setting StaysOpen="True" or by setting IsOpen=true in a click eventhandler or MouseButtonUp eventhandler.
Also using your example with the image, you could have a transparent area right above the image in your popup visualtree that 'captures' the MouseUp event when the popup is open. you would have to change the placement option and bind that area to the image's width and height properties.

Related

WPF Popup disappear on Custom contextmenu opening

I have a TextBox in a PopUp. On clicking a ToggleButton, the PopUp Window opens.
Toggle Button Code
<ToggleButton Name="popupButton" Content="Click Me To Open Popup!" >
<ToggleButton.Template>
<ControlTemplate TargetType="ToggleButton">
<Border Name="bg" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased" Background="Transparent" Width="17" Height="15">
<StackPanel HorizontalAlignment="Right" Width="17" >
<Path RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" Data="M186,52 C208,52 230,52 252,52" Fill="Black" Margin="0,1.5" Stretch="Fill" Stroke="#457faa" StrokeThickness="2" />
<Path RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" Data="M186,52 C208,52 230,52 252,52" Fill="Black" Margin="0,1.5" Stretch="Fill" Stroke="#457faa" StrokeThickness="2" />
<Path RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" Data="M186,52 C208,52 230,52 252,52" Fill="Black" Margin="0,1.5" Stretch="Fill" Stroke="#457faa" StrokeThickness="2" />
</StackPanel>
</Border>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
On clicking the ToggleButton it will open following the PopUp Window which contains the TextBox.
Popup Window Code
<Popup Name="testPopup" Placement="Bottom" PlacementTarget="{Binding ElementName=popupButton}" IsOpen="{Binding IsChecked, ElementName=popupButton}" StaysOpen="False">
<StackPanel Orientation="Horizontal">
<Label Content="Username: " Background="White"/>
<TextBox Text="Please RightClick to copy the text." ContextMenuOpening="TextBox_ContextMenuOpening">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Copy"/>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</StackPanel>
</Popup>
When a user clicks on the TextBox in the PopUp I want to call the ContextMenu opening event which is as following:
CodeBehind for ContextMenuOpening Event
private void OpenContextMenu(FrameworkElement e)
{
if (e.ContextMenu != null)
{
e.ContextMenu.IsOpen = true;
}
}
private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
e.Handled = true;
this.OpenContextMenu(sender as FrameworkElement);
}
Purpose to Call ContextMenuOpening
I'm calling its opening event because I want to stop the parent contextmenu to open by e.Handled = true. Then I want to open the ContextMenu of the TextBox (source element). Here comes the issue.
Issue
As soon as I open the ContextMenu from code-behind, it opens the ContextMenu but the PopUp disappears as StayOpen property is set to False. I want to use StayOpen to False, as it should close as soon as the focus is lost.

Need to make full screen popup in WPF

I want to open full screen popup but what is happening right now is that from the top it increases but bottom transparent or any color effect is not visible.Below is the code i am using .please let me know where i am wrong.Please see popup design CLICK HERE
<Popup StaysOpen="False" x:Name="popintaling" Width="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" Placement="Center" IsOpen="False" Height="900" Panel.ZIndex="1" HorizontalAlignment="Center" VerticalAlignment="Center" AllowsTransparency="True" PopupAnimation="Fade" ClipToBounds="True" OpacityMask="#FFFFAA00" RenderTransformOrigin="0,0" >
<Popup.Effect>
<DropShadowEffect/>
</Popup.Effect>
<Border Width="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" Height="900" BorderBrush="#7F1D1A1A" Margin="0,0,0,0" BorderThickness="300,300,300,300" HorizontalAlignment="Center" >
<Canvas Margin="0,100,0,90" Height="800" Width="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" Panel.ZIndex="999" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFF3800C" >
//some code here
</Canvas>
</Border>
</Popup>
A Popup that covers more than 75 percent of the screen, reduces its width first and then its height to meet the maximum coverage limit of 75 percent.You cannot cover make it full screen.
Read the documentation : PlacementMode
after InitializeComponent in constructor, set popup child width and height from code
(popintaling.Child as FrameworkElement).Height = Window.Current.Bounds.Height;
(popintaling.Child as FrameworkElement).Width = Window.Current.Bounds.Width;
A full screen popup is not a popup. Just put that code into a border with a Background color and handle the events yourself. Also to make your App fullscreen just set the WindowState to Maximized, and the WindowStyle to None

Prevent Thumb Dragging in Certain Regions

Below is a very crude start to a thumb template that eventually might look like a respectable popup window. Obviously the user can drag it around from any location therein. Is there any way to make it so it can only be dragged from the top AliceBlue border? Put another way, it is possible to disable dragging from the second border?
<Popup x:Name="MyPopup">
<Popup.Child>
<Thumb DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<StackPanel Margin="20">
<Border Height="20" Width="200" BorderBrush="Black" BorderThickness="2,2,2,0" CornerRadius="3,3,0,0" Background="AliceBlue"></Border>
<Border Height="200" Width="200" BorderBrush="Black" BorderThickness="2,0,2,2" Background="Bisque"></Border>
</StackPanel>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Popup.Child>
</Popup>
I haven't tested this so it's a bit of a guess. Add an event handler to the MouseLeftButtonDown event on AliceBlue Border. In the event handler:-
e.Handled = true;

WPF Popup UI showing black

I am using a WPF Popup control, and it is showing the background as black. I put a StackPanel inside it with Background="Transparent", but that does not help.
<Popup PlacementTarget="{Binding ElementName=parentStackPanel}" Placement="Center"
IsOpen="False" Name="m_popWaitNotifier" PopupAnimation="None"
AllowsTransparency="False">
<StackPanel Orientation="Vertical" Background="Transparent">
<uc:CircularProgressBar x:Name="CB" StartupDelay="0"
RotationsPerMinute="20"
Height="25" Foreground="White"
Margin="12"/>
</StackPanel>
</Popup>
How does one make the background on Popup transparent (or any color)?
You need to set the AllowsTransparency="True" Popup Property to True
Here is an example:
<Grid>
<StackPanel>
<Button Click="Button_Click" Width="100" Height="20" Content="Click" />
<Popup x:Name="popup" Width="100" Height="100" AllowsTransparency="True">
<Grid Background="Transparent">
<TextBlock Text="Some Text" />
</Grid>
</Popup>
</StackPanel>
</Grid>
and the click handler
private void Button_Click(object sender, RoutedEventArgs e)
{
popup.Visibility = System.Windows.Visibility.Visible;
popup.IsOpen = true;
}
The base color of a Popup, or a Window for that matter, is black. You rarely see it for a Window because Window has a Background property and it defaults to a solid color, but if you set Window.Background to Transparent it will also be black. But Popup doesn't have a Background property and so, pardon the pun, this problem "pops up".
If you want the Popup to be transparent, you need to set AllowsTransparency="True". However, if you want the Popup to be a solid color, the simplest approach is to make the child of the Popup a Panel that supports the Background property and set that property to the color you desire and then set the child of the Panel to be the content you intended for the Popup in the first place. I suggest Grid as it won't affect the layout of your Popup. It's only effect will be to give you the background color you desire.
Make sure that the allow transparency is set to true, vertical and horizontal alignments are centered, and the height and width are set to Auto.
For example:
<Popup Name="popup1" Placement="Top" PlacementTarget="{Binding ElementName=button2}" AllowsTransparency="True" Height="Auto" Width="Auto" Panel.ZIndex="1" HorizontalOffset="-5" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Height="92" HorizontalAlignment="Left" Margin="93,522,0,0" Name="stackPanelPop" VerticalAlignment="Top" Width="147">
</StackPanel>
</Popup>
Another possible cause:
using IsOpen="True" in markup before AllowTransparency="True"
Switching the order fixes it.
My guess is that the CircularProgressBar is actually causing the Black background. The only other way that this could happen is if there was a Style or something set on one of the controls (Popup or StackPanel or...).
Here is a quick-n-dirty example that shows a TextBlock in a popup when a checkbox is checked. The colors chosen are just to make sure things stand out visually:
<StackPanel x:Name="stackPanelLayout">
<StackPanel.Background>
<RadialGradientBrush Center="0.75, 0.75"
SpreadMethod="Reflect">
<GradientStop Color="LightBlue" Offset="0" />
<GradientStop Color="SeaGreen" Offset="0.5" />
<GradientStop Color="MidnightBlue" Offset="0.75" />
</RadialGradientBrush>
</StackPanel.Background>
<CheckBox x:Name="chkShowPopup"
FontSize="20"
Foreground="White"
Content="Show Popup" />
<Popup PlacementTarget="{Binding ElementName=stackPanelLayout}"
Placement="Center"
IsOpen="{Binding ElementName=chkShowPopup, Path=IsChecked}"
Name="m_popWaitNotifier"
PopupAnimation="Slide"
AllowsTransparency="True">
<StackPanel Orientation="Vertical" Background="Transparent">
<TextBlock Foreground="White" FontSize="30" FontWeight="Bold" Text="PopUp" />
</StackPanel>
</Popup>
</StackPanel>
So, two tests you can do to determine what is happening:
Replace the CircularProgressBar with a simple TextBlock or other control that you don't have a Style applied to.
Put the CircularProgressBar as a standalone control somewhere on your window, or on an otherwise blank test Window.
As per this article Why is my WPF Popup black and how do I get it positioned properly?
:
You need to set the AllowsTransparency property on the Popup to True, and set the PlacementTarget and Placement properties to control the position the Popup opens in.
As per the code in question:
<Popup PlacementTarget="{Binding ElementName=parentStackPanel}" Placement="Center" IsOpen="False" Name="m_popWaitNotifier" PopupAnimation="None" AllowsTransparency="False">
<StackPanel Orientation="Vertical" Background="Transparent">
<uc:CircularProgressBar x:Name="CB" StartupDelay="0" RotationsPerMinute="20" Height="25" Foreground="White" Margin="12"/>
</StackPanel>
</Popup>
the PlacementTarget is set to parentStackPanel, whereas the questioner has mentioned:
Hi Svetlozar: I tried this but it does
not work. For me though I do not have
a StackPanel outside the Popup, but I
have a StackPanel within the Popup
that holds a couple of control on it
The problem could be that Popup could not find the PlacementTarget 'parentStackPanel' because it does not exist.
The problem is that the grid is not orientation places it outside of the popup.
Remove VerticalAlignment and horizontalAlignment from all the controls inside the popup, and it will work correctly
Quite old, but may help someone: Add InitializeComponent(); in the constructor, it solved my problem:
class MyPopupClass : Popup {
/*
...
*/
public MyPopupClass () {
InitializeComponent();
/*
...
*/
}
/*
...
*/
}

WPF ToolTip containing buttons can not recieve Mouse events, alternative?

I want a formatbar like in Office 2010 with WPF:
Select Text and then click buttons on appearing tooltip to execute a command
How can I make that work?
The ToolTip window can't accept focus, use the Popup control instead. It's a bit more cumbersome, than a tooltip, because many useful properties are set to false by default, here is a tiny example:
<Popup x:Name="samplePopup" PopupAnimation="Fade" Placement="Mouse" AllowsTransparency="True" StaysOpen="False" >
<Popup.Child>
<StackPanel Margin="10" >
<TextBlock Text="Some Text" HorizontalAlignment="Center" />
<Button Content="Close" HorizontalAlignment="Center" />
</StackPanel>
</Popup.Child>
</Popup>

Resources