How to resize popup in right direction? - wpf

I have a popup for which the size changes dynamically to fit the content. When the size increases, popup width increased in left direction only until it reaches left edge of the window. But in my case, I want popup width to be increased in right direction.
Below is the code snippet I am using
XAML:
<StackPanel>
<TextBox x:Name="popupText" Margin="5"/>
<Button Content="Change PopupText" Click="Button_Click_1" Margin="5"/>
<Canvas x:Name="canvas" Width="200" Height="200" Background="Red">
<Rectangle x:Name="rect" Canvas.Top="50" Canvas.Left="50"
Width="50" Height="100"
Stroke="White" StrokeThickness="3"/>
<Popup x:Name="popup" AllowsTransparency="true"
IsOpen="True" Placement="Relative"
PlacementTarget="{Binding ElementName=canvas}"
PlacementRectangle="50,50,50,100"
HorizontalOffset="0"
>
<TextBlock x:Name="textBlock" FontSize="14" Background="Yellow"
TextWrapping="Wrap" >
This is a popup with a PlacementRectangle.
</TextBlock>
</Popup>
</Canvas>
</StackPanel>
C#:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.textBlock.Text += this.popupText.Text;
}
ScreenShots:
Before changing size
After changing size
Type anything in the textbox and click "Change PopupText" button. you can clearly see popup size increasing in left direction.
Is above one behavior of popup? Could I change this behavior and increase popup width in right direction?
Thanks in Advance,
Shobika.

You may want to have a look at custom popup placements.
Set Placement="Custom" on your popup and use the CustomPopupPlacementCallback property to reference a callback. In the callback, you can calculate preferred placements, relative to your placement target.

Related

Align elements horizontally to max width but wrap when parent squeezed

I have a Grid which contains two Buttons. I want the first button to align to left and the second one to the right (this is why I'm using Grid as the parent). When the whole window is squeezed so much that the buttons cannot be entirely visible, I want them to wrap - ie. first one stays aligned to left but the second one is moved to a new row (and preferably but not necessarily is still aligned to right). For that I'd use a WrapPanel but the WrapPanel will not fill the parent window and hence will not align the buttons to left and right.
Is there any way to get both - the horizontal alignment and wrapping?
My current code:
<Grid Grid.Row="2" Visibility="{Binding Attachments.Count, Converter={StaticResource GreaterThanGivenValueVisibilityConverter}, ConverterParameter=1}">
<Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" BorderThickness="0" Focusable="False" Command="{Binding ExpandAttachmentsPanel}">
</Button>
<Button Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Top" Height="30" BorderThickness="0" Focusable="False" Command="{Binding DownloadAllAttachmentsCommand}">
</Button>
</Grid>
Is there any way to get both - the horizontal alignment and wrapping?
There is no built-in Panel that gives you this behaviour but you should be able to handle the SizeChanged event of the WrapPanel and programmatically adjust the Margin of one of the Buttons. Something like this:
<WrapPanel x:Name="wp" Background="Yellow" SizeChanged="WrapPanel_SizeChanged">
<Button x:Name="btn1" Width="100">
<TextBlock>1</TextBlock>
</Button>
<Button x:Name="btn2" Width="100">
<TextBlock>2</TextBlock>
</Button>
</WrapPanel>
private void WrapPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
btn1.Margin = new Thickness(0, 0, Math.Max(0, wp.ActualWidth - btn1.ActualWidth - btn2.ActualWidth), 0);
}

WPF Tooltip binding and open programatically does not work

I have a Custom tooltip icon which also has its content bound to Datacontext. My need was to open Tooltop on mouse hover as well as on click event. So I used following code
<Image Source="/MainUI;component\Images\home\tooltip_info.png"
Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Right"
Name="ImageToolTip"
Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave">
<Image.ToolTip>
<ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142">
<TextBlock TextWrapping="WrapWithOverflow"
Style="{StaticResource ExcalFont-MSFD300}"
FontSize="14" Text="{Binding Tips}"
Width="300" Padding="15,15,15,15">
<TextBlock.Background>
<ImageBrush ImageSource="Images\home\popupbox.png" />
</TextBlock.Background>
</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>
Code Behind:
private void ImageToolTip_MouseUp(object sender, MouseButtonEventArgs e)
{
((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = true;
}
private void ImageToolTip_MouseLeave(object sender, MouseEventArgs e)
{
((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = false;
}
Now the issue is on mouse up It opens Tooltip but it does not bind the text.
This is working fine if I am using static text instead of Binding. What am I doing wrong?
In case I mouse hover on icon then it works fine and shows the content too. Thereafter everytime mouse click also shows the tooltip content. Not sure why mouse click do not work initially. –
ToolTip is not part of the VisualTree.
A problem that is similar to the problem you have is described here:
RelativeSource binding from a ToolTip or ContextMenu
One option to solve your problem would be something like this:
<Image Source="/MainUI;component\Images\home\tooltip_info.png"
Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Left"
Name="ImageToolTip"
Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
<Image.ToolTip>
<ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142" >
<TextBlock TextWrapping="WrapWithOverflow"
FontSize="14" Text="{Binding PlacementTarget.Tag.Tips,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToolTip}}"
Width="300" Padding="15,15,15,15">
<TextBlock.Background>
<ImageBrush ImageSource="Images\home\popupbox.png" />
</TextBlock.Background>
</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>

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

WPF Popup control on ImageMouseLeftButtonDown disappears immediately

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.

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();
/*
...
*/
}
/*
...
*/
}

Resources