In my WPF application, Need to show a Non-modal window.
I am using MVVM Light framework.
People are suggesting different libraries to do so but is there any suitable control to do that using MVVM Light or using WPF native library?
Need to keep that non-modal window always on top.
Thanks.
To create a non-modal window you won't have to use a framework. The WPF library has enough possibilities to create it.
The quickest solution is to create a new Xaml Window and within the properties of the Xaml Window code, you van specify the TopMost priority, and set it to true.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NonModalWindow" Height="300" Width="300" TopMost="True">
<Grid>
<!---- Some element defined in your window ---->
</Grid>
</Window>
Once you have created this window it is only a matter of calling it.
new NonModalWindow().Show();
And if you do want to make it into a Modal window you can use to following code.
new NonModalWindow().ShowDialog(); // wooah a contradiction in the code
Related
Both of these applications are rather old and have been built and maintained over several years by several people. At the moment, one of controls used in the WinForms project really needs to be displayed in the WPF project.
I've read about using WinForms controls in WPF projects, and for the most part if you're just instantiating a regular empty WinForm control, it seems relatively simple.
What I'm wondering is how you would best approach using part of a large project in another project? Ideally the WinForm control will be visible from within ONE of our WPF controls, on ONE tab, after having been sent and loaded the required data.
Here are some general guidelines.
From your WPF application, add project references to:
your WinForms project
WindowsFormsIntegration
System.Windows.Forms
Modify your XAML to include a WindowsFormsHost:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabItem Header="Old Form">
<WindowsFormsHost Name="WinFormsHost"></WindowsFormsHost>
</TabItem>
</TabControl>
</Grid>
</Window>
Instantiate your old Form and set it as the child of the WindowsFormsHost. Set TopLevel to false or it'll complain that "the child control cannot be a top-level form." Change the FormBorderStyle too, to prevent the Form's title bar from showing up and allowing the user to drag the Form around.
public MainWindow()
{
InitializeComponent();
WinFormsHost.Child =
new Form1 { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
}
You end up with something like this:
You can read more in "Walkthrough: Hosting a Windows Forms Control in WPF" and the MSDN documentation for the "WindowsFormsHost Class".
Metro template for WPF app. I want a Window which dont have any Window Command like minimize maximize and close. In normal WPf it can be done by WindowsStyle but how i can remove them in this template. And i also want to make the Window to be TopMost="True" and WindowState="Maximized". So all this settings are possible in this template ? Please advice ?
No need to thange templates, just set some properties.
MetroWindow has properties:
<controls:MetroWindow ...
Topmost="True"
WindowState="Maximized"
ShowTitleBar="False" // Hide colored tile bar, title header and icon
ShowCloseButton="False"
ShowMaxRestoreButton="False"
ShowMinButton="False"
ShowWindowCommandsOnTop="False" // No window commands on top of flyouts
which will remove title bar (icon+color), and you can remove buttons too.
I'm trying to develop Custom Window, which i can reuse in other applications.
I know that WPF cannot derive from XAML
I also tried to deploy it as Class Library, the code provided in this
video:
http://www.youtube.com/watch?v=EuhhL_NF-B0&feature=c4-overview&list=UUjwAVugYBMQemsMi9AD4SZA
, but still it does not read the XAML file.
I tried with code-behind to set the ControlTemplate, but as i read FrameworkElementFactory is deprecated...
All i want to do is, derive from Window, change the ControlTemplate, release it as Class Library... anyone can show me how or point me to the right direction?
In my opinion the best solution will be to create custom UserControl, and then load it from Window.xaml.
Once you created your user control just load it from Window like this:
<Window xmlns:my="clr-namespace:Styx.GUI.View" x:Class="Styx.GUI.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="800" Width="650" MinHeight="600" MinWidth="600">
<my:MainWindowUserControl />
</Window>
I'm updating a WPF in a project. As I received it, here is how the base window is specified:
<Window x:Class="SomeProject.ButtonForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SomeProject"
Title="ButtonForm" Height="647" Width="379" WindowStyle="None" ResizeMode="CanResizeWithGrip"
SnapsToDevicePixels="True" ShowInTaskbar="False" WindowStartupLocation="CenterScreen"
SizeChanged="Resized" Closing="Window_Closing" AllowsTransparency="True" >
Some main things are that WindowStyle is None, ResizeMode is CanResizeWithGrip and AllowsTransparency is True.
What I would like to do is make this window resizable from all four edges without grips. Is there a way to do this directly in the markup? I've seen some projects that implement this, but they involve whole separate files and complicated code-behind. Certainly there is a simpler way.
Check out MahApps.Metro. It may not be exactly what you're looking for, but it'll give your application an updated look with the functionality you're looking for and takes almost no effort.
You may also want to check out this question for some more discussion about custom window chrome.
You have to set ResizeMode to "CanResize" and AllowTransparency to "False" to be able to see the chrome to resize it.
I am trying to migrate an existing Winforms project into WPF. However: there are some user controls I need to leave as WinForm controls.
I have added a WinForms UserControl into a WPF Window. It consists of a RichTextBox and some buttons and labels. This is subclassed into various further user controls.
When I embed the UserControl into a WPF window it renders - but none of the buttons appear to do anything. When underlying processes update e.g. the RichTextBox it does not display the content. Yet when I inspect the textbox in debug I can see the content (though I have to click on 'base' to see this.)
[ One difference I have spotted - though it may not be relevant - is that when this control is on a WPF and non-working Visual Studio shows the object as 'sealed' but when in the original Winforms project when it is fully working it does not show as sealed. ]
I have added code to change the text in the labels - and they also firmly refuse to update: yet again I can see the text if I examine the label in debug mode.
This stack overflow question may address the same issue:
WindowsFormsHost Winform pdfviewer control problem
but the answer didn't make a lot of sense to me:
It mentioned replacing
new Window { Content = CreateContent(), Title = title }.Show();
But this is not a piece of code I recognise: I am using a xaml file with code behind and it's called up using
System.Windows.Application app = new System.Windows.Application();
app.Run(new FormWPFApp());
(where FormWPFApp is my name for the WPF window)
Here is the xaml header:-
<Window x:Class="ZedApp.FormWPFApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Printers="clr-namespace:ZedApp.UserControls.Printers"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="Conversion version" Height="661" Width="1559" Loaded="Window_Loaded">
Here is the xaml I use for the two UserControls (they both inherit from the same base class) :-
<WindowsFormsHost Height="430" HorizontalAlignment="Left" Margin="192,32,0,0" Name="windowsFormsHostTicketPrinter" VerticalAlignment="Top" Width="324" Grid.Row="1" Grid.Column="1">
<Printers:TicketPrinter x:Name="ticketPrinter">
</Printers:TicketPrinter>
</WindowsFormsHost>
<WindowsFormsHost Height="430" HorizontalAlignment="Left" Margin="522,32,0,0" Name="windowsFormsHostJournalPrinter" VerticalAlignment="Top" Width="324" Grid.Row="1" Grid.Column="1">
<Printers:JournalPrinter x:Name="journalPrinter">
</Printers:JournalPrinter>
</WindowsFormsHost>
[Another thing I have noticed is a method that clears the Rich Text Box on one of the windows starts kicking out errors of the following type if run under WindowsFormsHost in WPF -
"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
private void ClearRichTextBox(RichTextBox rtbToClear)
{
if (rtbToClear.IsHandleCreated)
{
if (rtbToClear.InvokeRequired)
{
this.Invoke(new Action<RichTextBox>(ClearRichTextBox), new object[] {rtbToClear});
return;
}
rtbToClear.Clear();
}
}
]
What is the likely cause of this behaviour and what do I need to do to get the elements within the User Control working?
Proper input interop with WinForms requires some cooperation between the host and the WPF input system. The topic Message Loops Between Win32 and WPF in the SDK explains this well. In your setup, the easiest way to make this happen is to use code like this:
Window w = new Window1();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(w);
w.Show();
ElementHost.EnableModelessKeyboardInterop() essentially registers an input hook with the WinForms Application object (which normally runs the message loop) and calls ComponentDispatcher.RaiseThreadMessage().