I am new to WPF so please understand:
I have a class of usercontrol that implements a screen (dialog) with a grid that contains controls. My code behind file performs a series of checks prior to opening the dialog. If the conditions aren't met, I want to destroy/exit/unload/close the usercontrol. I am using Windows and it seems that I cannot find the call to close the usercontrol. I read up on the questions that had answers such as closing from the parent... however when I try to find the parent via this.Parent, it returns null.
Any advice?
If your conditions aren't met, and you don't even show the usercontrol - don't initialize it.
If you can't avoid that, you have the option of calling Dispose() or letting the GC handle it.
To get the parent from a usercontrol I use this
var parentWindow = Window.GetWindow(this);
But I agree with Andreas if the conditions are not met don't even load it.
A thought could you bind the grids contents remove the usercontrol from the bound object maybe .
Jim
I am guessing you have added your user control in the xaml. That is why you need to even worry about unloading it. Try loading it in the code instead. have a container in your xaml such as a stack panel to hold the user control.
And do not load it unless you conditions aren't met.
eg:
xaml;
<UserControl x:Class="UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
>
<Grid>
<StackPanel x:Name="mypanel">
</StackPanel>
</Grid>
</UserControl>
code;
if(conditions are true) then
Dim myusercontrol = New ucMyControl()
Me.mypanel.Children.Add(myusercontrol)
end if
Related
I have a wpf user control that is hosted by a third party application as a non-modal dockable window. I would like to trigger an event with the window gets focus or when the mouse enters the window. This is a MVVM design.
How do I bind the event handler of the Grid (not datagrid) or the UserControl to a method on the viewmodel.
I want to avoid the xaml.cs code since I have to cast the datacontext into the expected viewmodel to make the call.
<UserControl x:Class="MVVMWithCommand.ChartExtentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMWithCommand"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" GotFocus="UserControl_GotFocus">
<UserControl.DataContext>
<local:MyViewModel/>
</UserControl.DataContext>
<Grid GotFocus="Grid_GotFocus">
There were two problems combined. The solution in the link didn't work because of another problem.
This only partially anwsered the question
WPF Binding UI events to commands in ViewModel
combined with this it did:
Could not load file or assembly 'System.Windows.Interactivity'
I have Popup window where dynamically attaching the UserControl. Here I need to set the focus for first control. If I use:
FocusManager.FocusedElement="{Binding ElementName=txtcode}"
on UserControl the cursor not coming. Could anyone help me to set focus on first element in the UserControl.
Thanks in advance.
I have just needed to do this, but not in a user control, so might not help. what I needed to do is to add the FocusManager bit in the Window tag:
<Window x:Class="xxxxx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" FocusManager.FocusedElement="{Binding ElementName=txtcode}">
And then make sure i correctly named the control:
<TextBox Name="txtcode" Text="" />
Although I'm new to wpf, and haven't worked with user controls yet.
I am just in the process of teaching myself WPF. I have reached the point of adding controls dynamically and have hit a brick wall on something really simple. I code that should create a button (shown below):
Button button = new Button() { Height = 80, Width = 150, Content = "Test" };
parentControl.Add(button);
My question is what is parentControl actually called? I am using the standard Visual Studio 2012 WPF template and my main window is called MainWindow. I have no objects in the Window besides what comes in the template
So far I have looked at:
WPF runtime control creation
Dynamic control creation in WPF
WPF MVVM Dynamic control creation
Dynamic creation of control
Where should I put WPF specific code when using MVVM?
Steps Of Control Creation Process WPF
Where to put code in (primarily) windowless WPF app?
The closest I have found it: WPF runtime control creation.
All of these questions just assume you know such a basic thing but I don't. Please help.
I think I understand your question. If your XAML code looks like:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
</Window>
Then your codebehind should be something like:
public MainWindow()
{
InitializeComponent();
Button button = new Button() { Height = 80, Width = 150, Content = "Test" };
//In case you want to add other controls;
//You should still really use XAML for this.
var grid = new Grid();
grid.Children.Add(button);
Content = grid;
}
However, I warmly suggest you to use XAML as much as you can. Furthermore, I wouldn't add controls from the constructor but I'd use the Loaded event of the window. You can add a handler to the event in codebehind from the constructor, or directly in XAML. If you wanted to have the same result as above in XAML, your code would be:
<Window
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 Height="80" Width="180" Content="Test"/>
</Grid>
</Window>
I have a slight issue creating a new window. The weird part is that the window seems to be created, but calling .Show() or .Activate() does nothing.
The code I'm using to create the window is:
TicketView tv = new TicketView();
tv.Activate();
I was originally trying to set the data context of the window at the same time, but I've taken that out to see if it was the issue, but I'm still seeing the same behaviour. Regardless, that's what the code is at this time. If anybody has any ideas, I'd be very grateful!
For reference - This is the ticket view XAML
<Window x:Class="Helpdesk.View.TicketView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TicketView" Height="300" Width="300">
<Grid>
</Grid>
</Window>
It depends on what you are trying to do. What I have done in the past if it is my main window is to use the Application.Run method. If I am trying to run another window from my application I use the ShowDialog method.
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)
{
}