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

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>

Related

How do I open/create a menu in WPf with Material Design

So I´m using Material Design and Caliburn micro.
I want to get used to Material Design and im new to WPF. But i have to use both because of work.
I want to have a Colorzone in the top of my form.
<materialDesign:ColorZone
Mode="PrimaryLight"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="4"
Padding="16">
<StackPanel
Orientation="Horizontal">
<ToggleButton
Style="{DynamicResource MaterialDesignHamburgerToggleButton}">
</ToggleButton>
<TextBlock
VerticalAlignment="Center"
Margin="16 0 0 0">
Material Design In XAML Toolkit
</TextBlock>
</StackPanel>
</materialDesign:ColorZone>
I got the hamburger button on the right and when i start it the Animation works but how do i open a drawer or a menu when the button is clicked and how do i close it when it is pressed again.
Where do i write the code what the button does?
So I found an Answer:
<materialDesign:ColorZone
Mode="PrimaryLight"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="4"
Padding="16">
<StackPanel
Orientation="Horizontal">
<ToggleButton
Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
CommandParameter="{x:Static Dock.Left}"
Style="{DynamicResource MaterialDesignHamburgerToggleButton}">
</ToggleButton>
<TextBlock
VerticalAlignment="Center"
Margin="16 0 0 0">
Material Design In XAML Toolkit
</TextBlock>
</StackPanel>
</materialDesign:ColorZone>
You have to Add the Command and a CommandParameter for the Hamburger Button.
Now we just need a Drawer that we can open:
<materialDesign:DrawerHost.LeftDrawerContent>
<StackPanel Margin="16" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Button
Margin="25,10,25,10"
VerticalAlignment="Top"
cal:Message.Attach="DoCoolStuff()"
Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
CommandParameter="{x:Static Dock.Left}">
BlaBlaBlaYour Button Text here
</Button>
<Button
</StackPanel>
</materialDesign:DrawerHost.LeftDrawerContent>
So now we got a Menubar and A Drawer but they wont work until we write the them both into the following:
<materialDesign:DrawerHost Grid.RowSpan="3" Grid.ColumnSpan="4" Margin="0,0,0,0" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"
Panel.ZIndex="1">
<materialDesign:ColorZone...>
<materialDesign:DrawerHost.LeftDrawerContent...>
</materialDesign:DrawerHost>
It is important that we give the DrawerHost Panel.ZIndex = 1 because when you have a button where the Drawer should open the button will be in the Foreground but now the Drawer will be.
And thats it, now we have a menubar with a Hamburgerbutton that opens a drawer.
A tip for the Button i Added it does not need the Command and the commandparameter but it help when you want to close the drawer when a button is pressed.
I hope i could help somebody

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.

Creating Popup programmatically in WP7

How to create the popup in program? e.g. I need to rename the file in phone App. How to do this by using popup in wp7?
Add a Popup element to your XAML and define the content using regular elements (as you would any other Page or UserControl. Set the Popup.IsOpen property to true to show the Popup and false to close the Popup. The following XAML shows an example that I use for showing in-application "toast" notifications with the Silverlight Windows Phone Toolkit
<Popup x:Name="_toast">
<Grid x:Name="_toastContainer"
VerticalAlignment="Bottom"
Width="{Binding ActualWidth, ElementName=LayoutRoot}">
<StackPanel Margin="14,10">
<TextBlock Text="{Binding Title}"
HorizontalAlignment="Stretch"
TextWrapping="Wrap" />
<TextBlock Text="{Binding Content}"
HorizontalAlignment="Stretch"
TextWrapping="Wrap" />
</StackPanel>
</Grid>
</Popup>
Take a look at the Input Prompt control in the Coding4Fun Windows Phone Toolkit

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

How to dynamically add controls to a Silverlight Button?

I'm working on a Silverlight Polling control. My goal is to have a Button that contains other controls, so that when a user clicks on this combination, stuff happens.
Here's the basic XAML:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
<StackPanel Grid.Row="1" Grid.Column="0" Height="Auto" Width="Auto" Style="{StaticResource StackPnl}">
<TextBox Text="this is a text box" Style="{StaticResource Answer}" />
<ProgressBar Style="{StaticResource PollProgress}" />
</StackPanel>
</Button>
That works fine, but assume I don't have the StackPanel, etc, defined:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
</Button>
But that I want to add the StackPanel, TextBox, and ProgressBar at runtime.
Looking at just the first hurdle that I've run into, how do I dynamically add the StackPanel to the Button control?
Alternatively, if someone knows of a better way to do this...
var panel = new StackPanel();
Button.Content = panel;

Resources