I want to show WPF control on top Winform Control.
I want that the WPF Button will appear on top on the Winform TextBox.
The result is that the WPF control is hidden in the back of the winform TextBox and I can't see it.
Why is that?
This is my code:
<UserControl x:Class="Philips.PmsCT.Host.Applications.ExamApplication.ScanRulerComponent.WPFHostWF"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Width="800" Height="120">
<Grid>
<WindowsFormsHost >
<wf:TextBox BackColor="LightBlue" />
</WindowsFormsHost>
<Button Width="100" Height="25" Background="Red"/>
</Grid>
You seem to have the Airspace problem.
So you can rearrange the two controls, that they are layouted next to each other, not on top of each other or use the Wpf TextBox or you have to really bend over backwards to mitigate the airspace problem all together.
All WPF controls render in one native window. You can check it via SPY++ for example.
Almost each WindowsForm Control renders in it own window. Again, you can check it via SPY++.
You want to produce behavior when one window A(WPF) is covered by another window B(Winform control). At the same time you want window B is covered by A(wpf button). I don't know if it can be possible without any hook.
Possible solutions is:
1. Host WPF inside WinForm Panel, where TextBox is.
2. Create WPF form with only required button and show it over first form.
In according to the Microsoft docs: "Visible WindowsFormsHost elements are always drawn on top of other WPF elements, and they are unaffected by z-order."
Related
I have a WPF form with ComboBox on it. If I expand my combobox, then open an app such as Notepad or any other, the app will cover my WPF form but the expanded ComboBox portion will still be on top. So it appears as the Notepad app is between the WPF form and its expanded portion of ComboBox (the combo box dropdown). It looks like the ComboBox will show its expanded dropdown always at the top of z-order.
This is causing few other issues such as beeing unable to close the WPF form.
This is simply wrong.
Above:
z-1 is z-order of my WPF form with expanded ComboBox (but note that the expanded area is on top of applications opened and overlapping the WPF form
z-2 is order of Notepad application opened and overlapping my WPF form with the ComboBox on it (again, you can see that Notepad overlaps WPF form but the ComboBox expanded dropdown overlaps Notepad. It should be behind Notepad, not in front of it)
z-3 is the expanded dropdown of my ComboBox located on WPF form with z-1 order
My ComboBox is part of UserControl and its xaml is like this
<ComboBox Name="MyComboBox"
ItemsSource="{Binding ItemsSet}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedItem="{Binding Path=SelectedItem}"
BorderBrush="Green"
BorderThickness="2">
</ComboBox>
How do I prevent the issue in the provides screenshot?
I understand that the behavior is in most cases "wrong"; when would you really want a dropdown to stay open if focus is lost on the current application? However, as I mentioned in my comment, that is the designed behavior of the Popup control. A WPF combobox under the hood is actually a ToggleButton with a Popup control that is set to open when the ToggleButton is toggled. So... the way to prevent the issue you're seeing is to prevent the Popup control from staying on top of all applications.
There's a StackOverflow question for that :)
Popup always stays on top
One of the answers actually has code snippet with a derived/custom popup control that uses user32.dll to prevent the undesired behavior.
So ok great, that's how you fix a Popup, but how do you fix a ComboBox? You simply have to override the default ControlTemplate to use the fixed-non-top-most Popup control instead of the standard WPF Popup.
I "fixed" this by setting the WPF form containing my ComboBox as TopMost="True" in its xaml. Not perfect fix but at least removes the stupid look and all other problems it leads to I discovered. I cannot believe that MS made this default behavior, it is plain stupid.
I'm trying to create a simple 'dialog'-type window in WPF. However, for this specific instance, I do not want the client area to have a border, or even a background for that matter. I just want my controls to appear over the background of the window the way they do with a simple MessageBox.
I played with the different values for WindowStyle but they all called out the client area with a color. I also tried simply setting the client's Background to transparent, but that didn't work either just rendering it in black.
Here's a crappy Photoshop job showing what I'm after:
Note: I'm not after the messagebox contents themselves--e.g. the icon, buttons and message, etc.--I'm only asking about how to suppress the client area from appearing in any window. I just happened to use a messagebox as an example as someone linked to it in their answer.
As you can see (or rather can't) there is no visible demarcation of the client area.
Used to be so simple in WinForms, but WPF has me stumped. Anyone?
I'm not sure what you're after. Do you want only the controls on your dialog to be visible with the dialog's border and background transparent? If so, try these settings on your dialog:
WindowStyle="None"
ShowInTaskbar="False"
AllowsTransparency="True"
Background="Transparent"
If you want your dialog's background color to the Winform System.Control with no border, set your form's Background like this (instead of Transparent):
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
(I am trying to learn WPF using tutorials and documentation, and trying to develop a user interface for my backend-complete application while I do say. I've heard people say that the learning curve is quite steep. But sometimes I wonder whether what I'm trying to do is actually something that's hard to do in WPF, or if it's simple but I'm thinking in wrong terms, or if it's neither, it's quite simple but I just happen not to know how.)
Here's my current question. I wanted clicking that clicking some part of my UI will bring up a 'popup' where the user can enter more information. I would like a 'lightbox-style' popup, i.e. the popup is modal to the page, it darkens the rest of the page to become the center of attention, etc. These are seen commonly on Web sites.
A bit of searching led me to the WPF Popup control. I added it, put my content in, set the IsOpen property to True, and -- presto! A popup. Then I added an invisible Rectangle that covers my whole window, and set it to Visible as well when I want my popup to open. Great!
So now I wanted to do this dynamically, because sometimes I will be loading a record which will sometimes have a need to open another control (a UserControl) in a popup to edit its information. So I made myself a method called OpenPopup. But I can't seem to find a way to write this method using WPF. In Windows Forms I'd have written: (I use VB.NET)
Sub ShowPopup (form as Form, ctrl as Control)
'Create 'rect' as new dark rectangle control
'Z-order it to the top
'form.Controls.Add 'rect'
'form.Controls.Add ctrl
'Z-order 'ctrl' to the top
'Center 'ctrl'
'Set focus to it
End Sub
But with WPF I run into problems:
1) I can't add it to the WPF window, because it already has a child.
2) If that child is a Canvas, that's not too bad. I can detect that, and add it to the Canvas. I have to find some way to set its Left, Top etc. properties and Width and Height, since those do not seem to be properties of the Rectangle control but rather extended by the Canvas object -- in XAML they're called Cavnas.Top etc. but Intellisense is not showing them when I try to use it in code.
3) But if it's a StackPanel? Then my rectangle will just be stacked below the other controls! And not covering them! Is there a way around this?
4) And if the window contains only one control and no container control at all?
5) I think there were more problems I ran into. But let's start with these.
Thanks in advance for your help.
1) I can't add it to the WPF window, because it already has a child.
Ah, the evils of codebehind. The solution is not to add it to the visual tree, it is to place it in the visual tree, ready and waiting to pounce, but hide it from the user's view.
Here's a sample you can drop in Kaxaml that demonstrates the point. Set the Lightbox Grid's Visibility to Hidden to access the hidden content.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Viewbox>
<TextBox Text="SIMULATING CONTENT" />
</Viewbox>
<Grid x:Name="Lightbox" Visibility="Visible">
<Rectangle Fill="Black" Opacity=".5"/>
<Border
Margin="100"
Background="white"
BorderBrush="CornflowerBlue"
BorderThickness="4"
CornerRadius="20">
<Viewbox Margin="25">
<TextBox Text="SIMULATING LIGHTBOX"/>
</Viewbox>
</Border>
</Grid>
</Grid>
</Page>
2) (snip) Intellisense is not showing them when I try to use it in code.
Canvas.Top etal are Attached Properties. Attached Properties are extremely convenient and easy to use in XAML, but they are very confusing and hard to use from code. Another reason why codebehind is evil.
3) But if it's a StackPanel? Then my rectangle will just be stacked below the other controls! And not covering them! Is there a way around this?
I redirect you back to 1. There are also many other container controls in WPF. You should investigate them and observe how they control layout. For instance, my use of the Grid was not to make use of its ability to block off sections of UI for controls, but for its ability to layer controls ontop of each other and to stretch them out to their maximum available size for the available space (the viewboxes are just there to zoom the controls instead of stretch them).
4) And if the window contains only one control and no container control at all?
The root of a window would almost always be a container control. But you control that, so if you needed to add controls to the visual tree at runtime you could easily ensure the child of the window is a container control you could deal with.
5) I think there were more problems I ran into. But let's start with these.
No kidding. My number one suggestion for people in your situation is to drop what you're doing and learn about MVVM. The Model-View-ViewModel is a very simple way to code WPF applications that takes advantage of many of the features of WPF--databinding, templating, commands, etc. It allows you to code your logic not in codebehind (RETCH) but in easy to create and test classes.
I want to create a custom dialog box-like control in Silverlight for WP7 that I can use this way:
<local:Dialog>
<StackPanel>
<TextBlock>Are you sure?</TextBlock>
<Button Content="Yes" Click="ClickCallback" />
</StackPanel>
</local:Dialog>
As in, just a simple container that I can add arbitrary content to. I just want to add storyboards for animations and a backdrop to make the dialog modal, etc. I already have this ready.
What I don't know how to do is add the content. I've read that you have to inherit from ContentControl, but how is this actually implemented?
There are number of good examples on ContentControl usage available in Silverlight Toolkit. For example Frame control located in Source\System.Windows.Controls.Navigation\System\Windows\Controls\Frame.cs
Greetings
I'm currently making an application in WPF as I'm fairly new to WPF I'm running into some difficulties. I have Googled my question but with no great success. This is the current situation, XAML of main window below:
<Grid Height="279" HorizontalAlignment="Left" Margin="166,0,0,0" Name="gridScoreboard" VerticalAlignment="Top" Width="808">
<!--Scoreboard Image-->
<Image Source="pack://application:,,,/Images/Style/Scoreboard.png" Width="517" Height="91" HorizontalAlignment="Left" Margin="138,1,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" />
<Canvas Name="canvasRacePlayer1" Width="14" Height="14" Canvas.Left="33" Canvas.Top="66" Background="Transparent" MouseLeftButtonDown="canvasRacePlayer1_MouseLeftButtonDown" Margin="171,70,623,195" />
<local:ucRaces HorizontalAlignment="Center" Margin="93,62,632,187" Width="78" Visibility="Hidden" x:Name="ucRacesP1" Height="33" />
</Grid>
The user control is hidden from the start (ucRaces), once the little canvas (canvasRacePLayer1) is clicked the user control will be shown. However I would like this user control to 'slide' from right to left from a certain point. As if it would become visible in small steps. I have found information for animations for rectangles and buttons but no success really for User Controls.
Thank you in advance
If you are going to create animations for your WPF project, I suggest that you use Expression Blend. You can design your program using EB and implement the functionality of it using Visual Studio. It will be hard to make animations, writing XAML syntax or C# code.
How would you be able to animate your user controls using EB? Well, it is actually very simple. You need to open your existing WPF project first. Then, go to File -> New Item -> User Control, and create the user control. Then, if you want to add it to your project, switch back to the WPF project currently open in EB and click the right arrows (>>) on the toolbar placed on the left-hand side of the screen and go to Project -> [Your User Control Here]. Now you have added it to your project.
If you want to animate the user control, you have to add a StoryBoard to your timeline. When you are on your WPF project in EB, under Objects and Timeline, click the plus (+) sign and add a new StoryBoard. Now, you have a timeline that you need to use to animate your user control. You can place KeyTime attributes on the timeline and define the path the user control is supposed to follow from location A to location B and also the level of opacity if you want the user control to gradually become visible.
You can add one more user control and implement its logic for the second user. Expression Blend will make your life easier.
Animating your UserControl shouldn't be much different from animating any other WPF object: You can either animate the margin (using a ThicknessAnimation), or drop your user control into a canvas of its own, then animate the Canvas.Left property of your user control. In the latter case, take care to put the property name in parenthesis: Storyboard.TargetProperty="(Canvas.Left)".