hello i am having some problem while using user control in wpf. i have single image in user control . i want to place this user control on my main window which contains some map.i am placing this user control(image) on my main window.they are 10 t0 15 .what i want is to open different windows by clicking on this usercontrol which is just an image . but when i do that it show me only same window every time . is there any way so i just have to create single user control and use it multiple times to open different windows .like (window1,window2,window3) etc..
kindly help me . i will be really thankfull.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Demo.WindowsPresentation.CustomMarkers.CustomMarkerDemo"
Height="40" Width="30" Opacity="10">
<Image MouseDown="clickit" Name="icon" Source="bigMarkerGreen.png" VerticalAlignment="Center" HorizontalAlignment="Center" />
In cs
this.MouseDown += new MouseButtonEventHandler(clickit);
void clickit(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("test");
main.show();
}
As far as I could understand by the given information, you want to open this UserControl in different windows. I would suggest you to create a window and add this UserControl onto it like
<Window x:Class="Demo.WindowsPresentation.CustomMarkers.win.winCustomMarker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:marker="clr-namespace:Demo.WindowsPresentation.CustomMarkers"
Title="Window" Height="345" Width="380">
<Grid>
<marker:CustomMarkerDemo></marker:CustomMarkerDemo>
</Grid>
</Window>
And then in your mouse click event:
void clickit(object sender, MouseButtonEventArgs e)
{
winCustomMarker customMarkerWindow = new winCustomMarker();
//bind any information you want that you want to pass to your inner UserControl
customMarkerWindow.show();
}
You may consider launching them as separate processes. Move user control to a new WPF project(.exe) and from main app create as many instances of user control you need.
var imgUserControlProcess = new ProcessStartInfo
{
FileName = "ImageUserControl.exe",
UseShellExecute = false,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Normal
};
var processStart = new Process
{
StartInfo = mainViewProcess
};
processStart.Start();
Related
I want to be able to show a preview of a screen saver inside WPF window. (using a container or control or ...) I know that Windows itself passes "/p" argument to the screen saver to get a preview. But how can I show that preview inside my WPF application? Should I get a handle of it and change its parent to my container o control? How?
You need to use Windows.Forms interop, because screen savers expect windows handles (HWND) and in WPF, only top-level windows have them.
MainWindow.xaml
<Window x:Class="So18547663WpfScreenSaverPreview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="Screen Saver Preview" Height="350" Width="525"
Loaded="MainWindow_OnLoaded" Closed="MainWindow_OnClosed"
SizeToContent="WidthAndHeight">
<StackPanel Orientation="Vertical" Margin="8">
<TextBlock Text="Preview"/>
<WindowsFormsHost x:Name="host" Width="320" Height="240">
<forms:Control Width="320" Height="240"/>
</WindowsFormsHost>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Diagnostics;
using System.Windows;
namespace So18547663WpfScreenSaverPreview
{
public partial class MainWindow
{
private Process saver;
public MainWindow ()
{
InitializeComponent();
}
private void MainWindow_OnLoaded (object sender, RoutedEventArgs e)
{
saver = Process.Start(new ProcessStartInfo {
FileName = "Bubbles.scr",
Arguments = "/p " + host.Child.Handle,
UseShellExecute = false,
});
}
private void MainWindow_OnClosed (object sender, EventArgs e)
{
// Optional. Screen savers should close themselves
// when the parent window is destroyed.
saver.Kill();
}
}
}
Assembly references
WindowsFormsIntegration
System.Windows.Forms
Related links
Walkthrough: Hosting a Windows Forms Control in WPF
Creating a Screen Saver with C# (describes command line arguments)
I'm trying to set up a WPF window so that it can accept different types of data via Drag and Drop. If I make a new project and set the window to the following:
<Window x:Class="DropShare.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" AllowDrop="True" DragEnter="Window_DragEnter">
<Grid>
</Grid>
</Window>
And set the code-behind to:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_DragEnter(object sender, DragEventArgs e)
{
}
}
I only ever get DragEnter firing for files. It never fires for anything else - text, images, etc.
Is there something I'm missing? All the tutorials I've read have seemed to suggest this is all that's needed as the DragEnter event handler let's me state what I accept.
So your code works fine for me. But try this...
In your Window:
<Label Background="Purple" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Drag from here!" MouseDown="Label_MouseDown"/>
and in your code behind:
private void Label_MouseDown(object sender, MouseButtonEventArgs e)
{
DragDrop.DoDragDrop(this, "This is just a test", DragDropEffects.All);
}
Then drag from the label into the window and see if your event fires.
If this works, it may have something to do with the permissions level between Visual Studio and your outside environment (possibly).
See:
https://superuser.com/questions/59051/drag-and-drop-file-into-application-under-run-as-administrator
In WPF drag and drop feature always has to deal with DragDrop Class, Please check here how to do drag and drop across applications
I have a SWF object embedded in a WindowsFormsHost Control inside a WPF window.
I'd like to add a toolbar over the swf movie.
The problem with the snippet of code I have below, is that when the new child is added to the host control (or the movie is loaded, I haven't figured out which yet), the toolbar is effectively invisible. It seems like the z-index of the swf is for some reason set to the top.
Here is what it looks like:
XAML:
<Grid Name="Player">
<WindowsFormsHost Name="host" Panel.ZIndex="0" />
<Grid Name="toolbar" Panel.ZIndex="1" Height="50"
VerticalAlignment="Bottom">
[play, pause, seek columns go here]
</Grid>
</Grid>
C#:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
flash = new AxShockwaveFlashObjects.AxShockwaveFlash();
host.Child = flash;
flash.LoadMovie(0, [movie]); // Movie plays, but no toolbar :(
}
Any insight on this issue would be much appreciated.
Update: Since no suitable answer was posted, I've placed my own solution below. I realize this is more of a hack than a solution so I'm open to other suggestions.
Here is my hackaround the WindowsFormsHost Z-index issue.
The idea is to place whatever you need to be overlayed nested inside a Popup. Then to update that popup's position as per this answer whenever the window is resized/moved.
Note: You'll probably also want to handle events when the window becomes activated/deactivated, so the pop disappears when the window goes out of focus (or behind another window).
XAML:
<Window [stuff]
LocationChanged="Window_LocationChanged"
SizeChanged="Window_SizeChanged" >
<Grid Name="Player">
[same code as before]
<Popup Name="toolbar_popup" IsOpen="True" PlacementTarget="{Binding ElementName=host}">
[toolbar grid goes here]
</Popup>
</Grid>
</Window>
C#:
private void resetPopup()
{
// Update position
// https://stackoverflow.com/a/2466030/865883
var offset = toolbar_popup.HorizontalOffset;
toolbar_popup.HorizontalOffset = offset + 1;
toolbar_popup.HorizontalOffset = offset;
// Resizing
toolbar_popup.Width = Player.ActualWidth;
toolbar_popup.PlacementRectangle = new Rect(0, host.ActualHeight, 0, 0);
toolbar_popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
}
private void Window_LocationChanged(object sender, EventArgs e)
{ resetPopup(); }
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{ resetPopup(); }
Another solution I've discovered is to use Windows Forms' ElementHost control. Since I'm using a Windows Form inside a WPF window anyway, why not just use an entire Windows Form and save myself Z-Issue headaches.
The ElementHost control is really useful, because I can still use my toolbar UserControl, and embed it inside the Windows Form. I've discovered that adding a child can be finicky with Windows Forms, so here's a snippet describing the solution:
First, toss in the ActiveX object, then an ElementHost Control, using the designer.
Form1.Designer.cs:
private AxShockwaveFlashObjects.AxShockwaveFlash flash;
private System.Windows.Forms.Integration.ElementHost elementHost1;
Form1.cs
public Form1(string source)
{
InitializeComponent();
toolbar = new UserControl1();
this.elementHost1.Child = this.toolbar;
this.flash.LoadMovie(0, source);
}
Note that the child was not set in the designer. I found that for more complex UserControls the designer will complain (though nothing happens at runtime).
This solution is, of course, still not entirely ideal, but it provides the best of both worlds: I can still code my UserControls in XAML, but now I don't have to worry about Z-indexing issues.
I have created a user control in wpf. I have also create a mainwindow. Now, what i want is that, when i click on a button (which is in mainWindow), a user control is shown like a dialog box. suppose i have a button named i-e create new user. Now, what i want is to show the control (that i have created for new user) on button click whithout calling it in mainWindow.
In the WPF demo application Family Show, user controls are created in the main window (MainWindow.xaml)
<!-- New User Control -->
<local:NewUserControl x:Name="NewUserControl"
HorizontalAlignment="Center" VerticalAlignment="Center"
AddButtonClick="NewUserControl_AddButtonClick"/>
Then in the code behind (MainWindow.xaml.cs), the different user controls are hidden or shown as a result of click actions from the button.
/// <summary>
/// Hides the New User Control.
/// </summary>
private void HideNewUserControl()
{
NewUserControl.Visibility = Visibility.Hidden;
DiagramControl.Visibility = Visibility.Visible;
enableButtons();
if (family.Current != null)
DetailsControl.DataContext = family.Current;
}
/// <summary>
/// Shows the New User Control.
/// </summary>
private void ShowNewUserControl()
{
HideFamilyDataControl();
HideDetailsPane();
DiagramControl.Visibility = Visibility.Collapsed;
WelcomeUserControl.Visibility = Visibility.Collapsed;
if (PersonInfoControl.Visibility == Visibility.Visible)
((Storyboard)this.Resources["HidePersonInfo"]).Begin(this);
NewUserControl.Visibility = Visibility.Visible;
NewUserControl.ClearInputFields();
NewUserControl.SetDefaultFocus();
... //Removed for brevity
}
I encourage you to download the Family Show app to look at the source code, or to browse it at least on-line.
You could put it in a separate window like Johannes Hofmeister suggested with his answer.
The main advantage of a User Control is that this user interface block might be used at multiple points in an application. (eg. a graph control user control with scroll, zoom and screenshot buttons would appear next to every graph, making it an ideal candidate for a user control).
You can easily add another window with the your usercontrol on it!
First, create another window (right click in the solution explorer, add new item, Window).
Second, drag you usercontrol onto the window:
<Window x:Class="MyWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:DeleteMeTest="clr-namespace:DeleteMeTest"
Title="Window1" Height="300" Width="300">
<Grid>
<MyWpfApplication:UserControl1 />
</Grid>
</Window>
Then you must setup the button click handler to show the window:
MainWindow.xaml:
<Window x:Class="MyWpfApplication.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>
<Button Content="Click Me" Click="Button_Click"/>
</Grid>
</Window>
The Button_Click Handler in MainWindow.xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e)
{
new Window1().ShowDialog();
}
The ShowDialog() Method opens a dialog, which means that the window comes on top and must be interacted with (Closed) before you can return to interacting with your main window.
You can also use the Show Method, to have a non blocking window.
I'm new in WPF and C#. I know a lot of VB.NET and I'm used to the way when I call a form object like textboxes, etc. I'm calling it from another form. Now, I'm using WPF, I'm confused. Because I have a Main Window. And I want to add and item to a listbox in the Main Window from a Class. In VB.Net , its just like this.
IN FORM2
Form1.Textbox.Text = "";
Wherein I can't do it in WPF. Can someone please Help me. Thanks!
WPF windows defined in XAML have their controls publicly accessible from other classes and forms, unless you specifically mark them with the x:FieldModifier attribute as private.
Therefore, if you make an instance of your main window accessible in another class, be it a Window or anything else, you'll be able to populate controls from within this second class.
A particular scenario is when you want to update the contents of a control in your main window from a child window that you have opened on top of it. Is such a case, you may set the child window's Owner property to the current, main window, in order to access it while the child is visible. For instance, let's say you have defined these two windows:
// MainWindow
<Window x:Class="TestApplication.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>
<ListBox Name="mainListBox" Height="250" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
<Button Content="Open Another Window" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="20" Click="OpenAnotherWindow_Click"/>
</Grid>
</Window>
and
// AnotherWindow
<Window x:Class="TestApplication.AnotherWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AnotherWindow" Height="300" Width="300">
<Grid>
<Button Content="Add New Item to Main Window" HorizontalAlignment="Center" VerticalAlignment="Center" Click="AddNewItem_Click"/>
</Grid>
</Window>
each in its own XAML file.
In MainWindow's code behind, inside the button click handler, you show an instance of AnotherWindow as a dialog and set its Owner property to MainWindow's instance:
private void OpenAnotherWindow_Click(object sender, RoutedEventArgs e)
{
AnotherWindow anotherWindow = new AnotherWindow();
anotherWindow.Owner = this;
anotherWindow.ShowDialog();
}
Now, you can access the MainWindow's instance from AnotherWindow's Owner property, in order to add a new item to the ListBox control defined on it, in the button click handler in AnotherWindow's code behind:
private void AddNewItem_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = Owner as MainWindow;
mainWindow.mainListBox.Items.Add(new Random().Next(1000).ToString());
}
It simply adds a new random number to the ListBox, in order to show how the code accesses and modifies the control's data in MainWindow.
Pure WPF solution, but also may be easiest in your case, is using a Data Binding in WPF.
Every form's control is binded to some data on ModelView (pure MVVM approach) or to data (more or less like yuo can do it in WindowsForms). So the "only" thing you have to do is to read/write data binded to controls on UI of that form.
For example, you have TextBox on Windows and want to read a data from it.
This TextBox is binded to some string property of the class that is responsible for holding the data for the controls on that form (just an example, in real world could be 1000 other solutions, based on developer decisions). So what you need, is not to say: "window give textbox" and after read TextBox's content, but simply read binded string property.
Sure it's very simply description of a stuff. But just to give you a hint. Follow databinding link provided above to learn more about this stuff. Do not afraid of a lot of stuff there, it's after all is not a complicated idea and also pretty intuitive. To make that stuff to work in simply case you will not need to make huge efforts by me. The stuff becomes really complex when you end up into real world applications.
This will get all active windows:
foreach (Window item in Application.Current.Windows)
{
}