How to add a radio button into a stackpanel from another Window? - c#

I'm new to wpf and c#. I have two windows like MainWindow and MainWindow2.
I have a button in MainWindow that opens the MainWindow2 and a GroupBox with a StackPanel.
In the MainWindow2 I have a button that I want to add a RadioButton to the StackPanel in the MainWindow.
Here's the MainWindow code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
RadioButton butãoRadial = new RadioButton();
stackPanel1.Children.Add(butãoRadial);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow2 obj = new MainWindow2();
obj.Show();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
}
}
Here's the MainWindow2 Code:
public partial class MainWindow2 : Window
{
public MainWindow2()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow window = new MainWindow();
}
}
Sorry for my bad english ^^

Please do not do it that way.... every time you add controls directly to a window from another window a kitten dies. If you're not into kittens then substitute something else you hold dear into that sentence.
A more appropriate way to do this is to bind an ItemsControl (or a derivative like a ListView) to an ObservableCollection in your ViewModel, then use an DataTemplate defined in the ItemTemplate property to represent each item in the ObservableCollection on the screen. Then all you have to do is add the appropriate data item to the ObservableCollection (which can be passed to the second window).
Pleeeeeeeeaaaassssseee don't be adding controls from another window!

Related

How to exchange objects between UserControlWPF and WindowsForm?

I have an old WinForm that combines 2 objects, but almost all metadata of the result is lost after the combining.
To solve this I created a UserControl WPF and I am using it as an element host inside the mainwindow Form.
I would like to import these 2 objects to UC and after the user selected the metadata it should return the output to the mainwindow where the metadata and the data should be integrated.
One other problem I have is to Hide and Show the UserControl.
From the MainWindow Form:
private Ch FirstCh = new Ch();
private Ch SecondCh = new Ch();
public WinForm()
{
InitializeComponent();
elementHost1.SendToBack();
elementHost1.Hide();
}
private void UserCWPFButton_Click(object sender, EventArgs e)
{
elementHost1.Show();
elementHost1.BringToFront();
Ch ThirdCh = new Ch();
//Would like to use the ThirdCh from UserCWPF here
}
From the UserControl:
public partial class UserCWPF : UserControl
{
public UserCWPF()
{
InitializeComponent();
//Would like to use FirstCh and SecondCh here
}
private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
this.Visibility = Visibility.Collapsed;
this.Visibility = Visibility.Hidden;
//Both of these leave me with a Gray square in front of the form!
}
}
So far I can only properly hide the UCwpf with .SendToBack, but I don't know how to do it within the UserCWPF.
Also, Does it make sense to use a User Control WPF in an old WinForm or it should just be another form?

textbox on mainwindow has to change the content of a label on a second window in wpf

So I have to transform the content of a label on a second window to the input of a textbox on the main window. I have no problem doing this on the same window, but I have no idea how to get the data across multiple windows. So far I've multiple things but I couldn't get it to work. This is my code at the moment:
public void Button_Click(object sender, RoutedEventArgs e)
{
label1.Content = textBox1.Text;
label2.Content = textBox2.Text;
}
So I want to do the same thing like this, only then the textBoxes are on the MainWindow and the Labels are on Window1.
Is there an easy way to do this? If not, what would be a better alternative?
If you are displaying Window1 from the MainWindow, you could inject it with a reference to the MainWindow when you create an instance of it:
public partial class Window1 : Window
{
private readonly MainWindow _mainWindow;
public Window1(MainWindow mainWindow)
{
InitializeComponent();
_mainWindow = mainWindow;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
label1.Content = _mainWindow.textBox1.Text;
label2.Content = _mainWindow.textBox2.Text;
}
}
MainWindow:
Window1 win = new Window1(this);
win.Show();
You could also get a reference to the MainWindow from Window1 like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
if (mainWindow != null)
{
label1.Content = mainWindow.textBox1.Text;
label2.Content = mainWindow.textBox2.Text;
}
}
But I recommend you to learn the MVVM design pattern. It is the recommended pattern to use when developing XAML based UI applications.
The simplest way is to include a reference to the Window1 instance on your MainWindow. The you can write ...
public void Button_Click(object sender, RoutedEventArgs e)
{
Window1.label1.Content = textBox1.Text;
Window1.label2.Content = textBox2.Text;
}
... although this can soon evolve into a big ball of spaghetti.
A more elegant solution is to create a ViewModel class with the required string properties, set the DataContext of both MainWindow and Window1 to the same instance of this class, and then bind the TextBoxes and Labels to these properties. No button click is required to keep all of the controls in sync.

WPF passing text from one window to another window

I have a main window and it has a button. When I press the main window button, it will open sub-window. In sub window, I have a text box and when I enter the text and click the add button in sub-window, the text should display on the main window. How do I achieve this? Thanks in advance.
You can create parameterized constructor of MainWindow class and pass value of Textbox from subwindow to main window and in MainWindow you can set content to that label.
Here is the implementation
MainWindow.xmal.cs
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent();
}
public MainWindow (string text) : this()
{
label.Content = text;
}
private void button_Click (object sender, RoutedEventArgs e)
{
Window1 win1 = new Window1();
win1.Show();
this.Close();
}
}
Here is the code for subWindow i.e. Window1.xaml.cs
public partial class Window1 : Window
{
private string text;
public Window1 ()
{
InitializeComponent();
}
private void button_Click (object sender, RoutedEventArgs e)
{
text = textBox.Text;
MainWindow mainWindow = new MainWindow(text);
mainWindow.Show();
this.Close();
}
}
You can simply modify the constructor of the second window :
public partial class Window1 : Window
{
string text;
public Window1 (string _text)
{
InitializeComponent();
this.text = _text;
}
}
I would add an event to your 'dialog' subwindow which the MainWindow can subscribe to as it instantiates it.
DialogWindow: (subwindow):
public class DialogInputEventArgs : EventArgs
{
public string Input { get; set; }
}
public partial class DialogWindow : Window
{
public event EventHandler<DialogInputEventArgs> InputChanged = delegate { };
private void SubmitInputButton_Click(object sender, RoutedEventArgs e)
{
InputChanged(this, new DialogInputEventArgs() { Input = this._inputTextBox.Text });
}
}
MainWindow:
private void ShowDialogButton_Click(object sender, RoutedEventArgs e)
{
DialogWindow dw = new DialogWindow();
dw.InputChanged += OnDialogInputChanged;
dw.Show();
}
private void OnDialogInputChanged(object sender, DialogInputEventArgs e)
{
// update the MainWindow somehow using e.Input (the text submitted in dialog)
}
If you need this mechanism for multiple windows I would go with something more generic, like a messagebus or observerpattern thing.
I'd suggest you to use CaliburnMicro framwework to achieve easier and better communication between your controls (assuming you are using MVVM pattern). You don't have to implement all features of CaliburnMicro, just EventAggregator to manage sending and handling messages between your controls.

Clear panel from usercontrol C#

I have a panel in a form and 2 UserControl, When the form loads, in the load () method, loads the first usercontrol using this code:
private void OlvidadaContraseña_Load(object sender, EventArgs e)
{
panel1.Controls.Clear();
UserControl1 Env = new UserControl1();
panel1.Controls.Add(Env);
}
How can I clean the panel (form panel) and load the second usercontrol from the first usercontrol?(Access form control from the usercontrol?)
Thanx
if you want change clean panel and load other usercontrol with first usercontrol you must use delegate-event.
you mast add on control (button,..) in first usercontrol
in code behind first userControl :
public partial class EnvioContraseña: UserControl
{
public delegate void LoadOtherUserControl(EnvioContraseña sender);
public event LoadOtherUserControl On_SelectButton;
public EnvioContraseña()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (On_SelectButton != null)
On_SelectButton(this);
}
}
then in code behind mainform
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.Clear();
EnvioContraseña Env = new EnvioContraseña ();
Env.On_SelectButton += Env_On_SelectButton;
panel1.Controls.Add(Env);
}
void Env_On_SelectButton(EnvioContraseña sender)
{
panel1.Controls.Clear();
UserControl1 uc1 = new UserControl1();
panel1.Controls.Add(uc1);
}

How to get control to previous window while current window is open in c# wpf

I have two windows: window1 and window2. When I click a button in window1, window2 gets displayed.
How can I get focus/control onto window1 without closing window2? I am working on c#.net wpf using VS 13
In Window1:
private void go_Click(object sender, RoutedEventArgs e)
{
Window2 r = new Window2(s1);
r.ShowDialog();
}
In Window2:
public partial class Window2 : Window
{
static string data;
public Window2(String a)
{
data=a;
InitializeComponent();
TextBox.Text = data;
}
}
You can pass a reference of window1 to window2 when you construct it.
Then from your window2, you can invoke the Activate method on window1 to bring it to foreground again:
window1.Activate();
See MSDN documentation of this method.
EDIT: Since you're using ShowDialog instead of Show (you didn't mention this information at first in your question), things are a bit different: ShowDialog is supposed to display the window as a modal dialog, and thus you cannot give the focus back to window1 once window2 is open, you have to close window2 first.
I'm not sure what behavior you'd like to achieve, but you should try to use Show instead of ShowDialog, and then use the Activate method as described above.
Change this
private void go_Click(object sender, RoutedEventArgs e)
{
Window2 r = new Window2(s1);
r.ShowDialog();
}
change the constructor of Window2 add reference of window1
Something like this
public partial class Window2 : Window
{
static string data;
static Window window1;
public Window2(String a,Window parent)
{
data=a;
InitializeComponent();
TextBox.Text = data;
window1 = parent;
}
}
and change that method to this
private void go_Click(object sender, RoutedEventArgs e)
{
Window2 r = new Window2(s1, this);
r.Show();
}
Now from Window2 call this
window1.Activate();
from any method

Categories

Resources