INotifyPropertyChanged with 2 Windows - c#

I have a simple WPF application with 2 windows.
I am trying to update a textblock and a checkbox on MainWindow when a user input something on SubWindow.
There are two variables that I set to pass values between windows.
Properties.Settings.Default.strText
Properties.Settings.Default.isChecked
MainWindow.xaml
<Window x:Class="PropertyChangedExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PropertyChangedExample"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400"
Focusable="False">
<Grid>
<TextBlock x:Name="TxtBlock" Text="{Binding TxtBinding}" Width="200" Height="30" Margin="0,100,0,0"/>
<CheckBox IsChecked="{Binding IsChecked}" Width="25" Height="25" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.ComponentModel;
using System.Windows;
namespace PropertyChangedExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SubWindow subWindow = new SubWindow();
subWindow.Show();
DataContext = new DataContextExample();
}
}
public class DataContextExample : INotifyPropertyChanged
{
public string TxtBinding
{
get { return Properties.Settings.Default.strText; }
set
{
Properties.Settings.Default.strText = value;
OnPropertyChanged(nameof(TxtBinding));
}
}
public bool IsChecked
{
get { return Properties.Settings.Default.isChecked; }
set
{
Properties.Settings.Default.isChecked = value;
OnPropertyChanged(nameof(IsChecked));
}
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
SubWindow.xaml
<Window x:Class="PropertyChangedExample.SubWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PropertyChangedExample"
mc:Ignorable="d"
Title="SubWindow" Height="100" Width="250">
<Grid>
<TextBox KeyUp="Window_KeyUp" x:Name="TxtBox" Width="200" Height="30" />
</Grid>
</Window>
SubWindow.xaml.cs
using System.Windows;
using System.Windows.Input;
namespace PropertyChangedExample
{
/// <summary>
/// Interaction logic for SubWindow.xaml
/// </summary>
public partial class SubWindow : Window
{
public SubWindow()
{
InitializeComponent();
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Properties.Settings.Default.strText = TxtBox.Text;
}
else if (e.Key == Key.LeftCtrl)
{
Properties.Settings.Default.isChecked = true;
}
else if (e.Key == Key.LeftShift)
{
Properties.Settings.Default.isChecked = false;
}
}
}
}
When a user enter anything on the textbox on SubWindow, the textblock on MainWindow should be updated. Also, the checkbox should be either checked and unchecked if a user press either left ctrl or shift key. So far, nothing happens. What am I missing here?

You need to pass the DataContext to your second window, or else it has no way of knowing about the data used in your MainWindow. This can be done in the second window's constructor.
DataContextExample myContext;
public SubWindow(DataContextExample context)
{
myContext = context;
DataContext = myContext;
InitializeComponent();
}
Then call it like so in from your MainWindow
public MainWindow()
{
InitializeComponent();
DataContextExample context = new DataContextExample();
SubWindow subWindow = new SubWindow(context);
subWindow.Show();
DataContext = context;
}
Also, in your second window you are changing Properties.Setting.Default values directly, but you need to change the DataContext value.
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
myContext.strText = TxtBox.Text;
}
//....
}

The best solution i can see for this kind of situation is messagin or know as Publisher/Subscriber pattern, in your sub window you create an event and publish it to whatever lessner (subscriber). here is a simple example :
https://www.c-sharpcorner.com/UploadFile/pranayamr/publisher-or-subscriber-pattern-with-event-or-delegate-and-e/

Related

Raise a event on the User control from the Main Window in WPF

There is a textbox in my mainwindow.xaml, when I enter the textbox, I expect the label in my usercontrol, known as View1.xaml will be update accordingly. However I realise the event is not raise at all in the user control when I type the textbox, can you tell me which part is wrong?
The event is able to raise in TextBox_TextChanged_1
my MainWindow.XAML
<Window xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:testapplication" x:Class="testapplication.MainWindow"
Title="MainWindow" Height="964" Width="790">
<Grid >
<Button x:Name="OpenView1" Content="Open Window 1" HorizontalAlignment="Left" Margin="33,70,0,0" VerticalAlignment="Top" Width="111" RenderTransformOrigin="0.279,1.409" Click="OpenView1_Click"/>
<Button x:Name="OpenView2" Content="Open Window 2" HorizontalAlignment="Left" Margin="33,169,0,0" VerticalAlignment="Top" Width="111" Click="OpenView2_Click"/>
<Button x:Name="OpenView3" Content="Open Window 3" HorizontalAlignment="Left" Margin="33,259,0,0" VerticalAlignment="Top" Width="111" Click="OpenView3_Click"/>
<local:View1 x:Name="ViewOne" HorizontalAlignment="Left" Margin="33,332,0,0" VerticalAlignment="Top" Height="226" Width="204" Visibility="Hidden"/>
<local:View2 x:Name="ViewTwo" HorizontalAlignment="Left" Margin="284,332,0,0" VerticalAlignment="Top" Height="226" Width="208" Visibility="Hidden"/>
<local:View3 x:Name="ViewThree" HorizontalAlignment="Left" Margin="534,332,0,0" VerticalAlignment="Top" Height="226" Width="196" Visibility="Hidden"/>
<TextBox HorizontalAlignment="Left" Height="42" Margin="326,70,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="182" FontSize="22" TextChanged="TextBox_TextChanged_1"/>
</Grid>
</Window>
my MainWindow.cs
namespace testapplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
//InitializeComponent();
}
//event handler
public event EventHandler<EventArgs> changedText;
private void OpenView1_Click(object sender, RoutedEventArgs e)
{
ViewOne.Visibility = Visibility.Visible;
}
private void OpenView2_Click(object sender, RoutedEventArgs e)
{
ViewTwo.Visibility = Visibility.Visible;
}
private void OpenView3_Click(object sender, RoutedEventArgs e)
{
ViewThree.Visibility = Visibility.Visible;
}
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (changedText != null)
{
changedText(this, e);
}
}
}
}
This is my UserControl, known as View1.xaml, it is included in my MainWindow.Xaml
namespace testapplication
{
/// <summary>
/// Interaction logic for View1.xaml
/// </summary>
public partial class View1 : UserControl
{
private MainWindow newWindow = new MainWindow();
public View1()
{
InitializeComponent();
newWindow.changedText += newWindow_ChangeText;
}
void newWindow_ChangeText(object sender, EventArgs e)
{
ViewOnelabel.Content = "Happy";
}
}
}
The problem is my ViewOnelabel.Content = "Happy" did not execute at all, it remain unchanged
There are a few things I would like to point out.
The equivalent of a winforms label in wpf is a TextBlock. A wpf label is actually a type of contentcontrol. Hence the content property.
In wpf there are routed events. These "bubble" up ( and tunnel down ) the visual tree. That means you can handle an event in the window from a control in a usercontrol inside it.
But mainly.
I encourage you to look into the MVVM pattern.
I've put together some code which illustrates these points.
I'd recommend just using binding and mvvm though.
My MainWindow markup:
Title="MainWindow" Height="350" Width="525"
TextBoxBase.TextChanged="Window_TextChanged"
>
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<StackPanel>
<Label Name="OutputLabel"/>
<TextBlock Text="{Binding OutputString}"/>
<local:UserControl1/>
</StackPanel>
</Grid>
Notice that it handles a textchanged event and because that's routing it will get the event from UserControl1 inside it.
Code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_TextChanged(object sender, TextChangedEventArgs e)
{
OutputLabel.Content = $"Happy {((TextBox)e.OriginalSource).Text}";
}
}
You don't do anything with the text from your textbox in your handler but I have some code there proves you could get at that from mainwindow if you wanted.
My viewmodel:
public class MainWindowViewModel : INotifyPropertyChanged
{
private string inputString;
public string InputString
{
get { return inputString; }
set
{
inputString = value;
OutputString = $"{inputString.Length} characters entered";
RaisePropertyChanged();
}
}
private string outputString;
public string OutputString
{
get { return outputString; }
set
{
outputString = value;
RaisePropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Usercontrol1 just has a textbox:
<Grid>
<TextBox Text="{Binding InputString, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
As you type in that textbox, the text is transferred to the bound property in my viewmodel. That hits the code in my setter. This in turn sets OutputString which is bound to my textblock.
Text changes in both my label and textblock as I type.
Here's a link to my sample on onedrive
https://1drv.ms/u/s!AmPvL3r385QhgpgOPNKPs-veFJ2O3g
The main problem here is that your View1 class is subscribing to an event on a new MainWindow instance, not the MainWindow instance created by your application on start.
Since your MainWindow class has a reference to your View1 class (a named member "ViewOne") you should just change it from the MainWindow class.
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
ViewOne.ViewOneLabel.Content = "Happy";
}
Get rid of the chenagedText event handler and all the code in the View1.xaml.cs... you don't need it.
Note: I am hoping that you are just playing around and learning here... there is no way I would condone building a WPF application in this way.
You could only use the event of the MainPage. I recomment you to add a Property to the UserControl. In my case I call it Text.
public string Text
{
set { ViewOneLabel.Content = value; }
}
In the MainWindow use the Property within the TextChanged Event.
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
OpenView1.Text = TextBox.Text;
}
You are creating a new instance of MainWindow in your UserControl. What you want to do is to hook up an event handler to the instance that you actually see on the screen. You can get a reference to this one using the Window.GetWindow method:
public partial class View1 : UserControl
{
public View1()
{
InitializeComponent();
Loaded += (s, e) =>
{
Window mainWindow = Window.GetWindow(this) as MainWindow;
if(mainWindow != null)
mainWindow.changedText += newWindow_ChangeText;
};
}
void newWindow_ChangeText(object sender, EventArgs e)
{
ViewOnelabel.Content = "Happy";
}
}

Datagrid inside a Custom control is not updating

In my Custom Control that have a DataGrid and two buttons, one for add rows in this DataGrid and other button to remove elements.
(because of my reputation I can not post an image here, sorry!) :-(
My Custom Control code behind:
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class CustonDatagrid : UserControl
{
public CustonDatagrid()
{
InitializeComponent();
}
#region DependencyProperty Content
/// <summary>
/// Registers a dependency property as backing store for the Content property
/// </summary>
public static readonly DependencyProperty ColectionProperty =
DependencyProperty.Register("Colection",
typeof(ObservableCollection<object>),
typeof(CustonDatagrid),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsParentMeasure));
/// <summary>
/// Gets or sets the Content.
/// </summary>
/// <value>The Content.</value>
public ObservableCollection<object> Colection
{
get { return (ObservableCollection<object>)GetValue(ColectionProperty); }
set { SetValue(ColectionProperty, value); }
}
#endregion
public static readonly RoutedEvent AddButtonEvent = EventManager.RegisterRoutedEvent(
"AddButtonClick",
RoutingStrategy.Bubble,
typeof (RoutedEventHandler),
typeof (CustonDatagrid));
public event RoutedEventHandler AddButtonClick
{
add { AddHandler(AddButtonEvent, value); }
remove { RemoveHandler(AddButtonEvent, value);}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var newEventArgs = new RoutedEventArgs(AddButtonEvent);
RaiseEvent(newEventArgs);
}
}
My .xaml:
<UserControl x:Class="WpfCustomControlLibrary1.CustonDatagrid"
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" Name="CustonDataGrid">
<Grid>
<DockPanel LastChildFill="True" >
<StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" Orientation="Horizontal">
<Button Margin="5" Width="20" Click="ButtonBase_OnClick" >+</Button>
<Button Margin="5" Width="20">-</Button>
</StackPanel>
<DataGrid ItemsSource="{Binding ElementName=CustonDataGrid, Path=Colection}" DockPanel.Dock="Top"></DataGrid>
</DockPanel>
</Grid>
</UserControl>
And the usage in a wpf windows:
xaml code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfCustomControlLibrary1="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=Model}"
>
<Grid>
<wpfCustomControlLibrary1:CustonDatagrid Colection="{Binding Path=Colection}" AddButtonClick="CustonDatagrid_OnAddButtonClick">
</wpfCustomControlLibrary1:CustonDatagrid>
</Grid>
</Window>
And the code behind + View Model + datagrid row view model:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
Model = new Model();
InitializeComponent();
}
public Model Model { get; set; }
private void CustonDatagrid_OnAddButtonClick(object sender, RoutedEventArgs e)
{
Model.AddElement();
}
}
public class Model : INotifyPropertyChanged
{
public ObservableCollection<DataGridRowModel> Colection { get; set; }
public void AddElement()
{
if (Colection == null) Colection = new ObservableCollection<DataGridRowModel>();
Colection.Add( new DataGridRowModel()
{
Name = "Test"
});
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DataGridRowModel
{
public string Name { get; set; }
}
The problem I am having is that the Datagrid is not showing the new elements added to the Collection. When debugging I can see my collection with many elements (one for each time I click on (+) button) but this elements are not showed in view.
Can some one give a tip where I am making a mistake or (probably) missing code?!?
Thanks.
One minor mistake made by you in CustonDatagrid.xaml
<DataGrid ItemsSource="{Binding ElementName=CustonDataGrid, Path=Colection}" DockPanel.Dock="Top"></DataGrid>
There is no element called CustonDataGrid because of which the elements were never reflected.
Changed it to
<DataGrid ItemsSource="{Binding Path=Colection}"></DataGrid>
I also made minor change in your MainWindow.cs
public partial class MainWindow : Window
{
public MainWindow()
{
Model = new Model();
InitializeComponent();
this.DataContext = Model;
}
public Model Model { get; set; }
private void CustonDatagrid_OnAddButtonClick(object sender, RoutedEventArgs e)
{
Model.AddElement();
}
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfCustomControlLibrary1="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
Title="MainWindow" Height="350" Width="525">
<ScrollViewer>
<wpfCustomControlLibrary1:CustonDatagrid Colection="{Binding Colection,Mode=TwoWay}" AddButtonClick="CustonDatagrid_OnAddButtonClick">
</wpfCustomControlLibrary1:CustonDatagrid>
</ScrollViewer>
</Window>
Added a constructor for Model.cs
public class Model : INotifyPropertyChanged
{
public ObservableCollection<DataGridRowModel> Colection { get; set; }
public Model()
{
Colection = new ObservableCollection<DataGridRowModel>();
}
public void AddElement()
{
Colection.Add(new DataGridRowModel { Name = "Test" });
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
I hope it works at your end also.

Databinding and then calling from another class in WPF

My purpose is to add a textblock to my main UI window, of which text will be updated if needed. For that, in my UIWindow xaml I did like this:
<Window x:Class="UIDesigner.UIWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:UIDesigner"
xmlns:c="clr-namespace:UIDesigner.Controls"
WindowStartupLocation="CenterScreen"
WindowState="Maximized"
WindowStyle="SingleBorderWindow"
Title="GUI"
Height="1000" Width="1400"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Icon="Resources/Images/Logo.png"
>
<Grid Margin="0">
<Grid Grid.Row="1" Margin="0,10,0,0">
<GroupBox Header="Console" Grid.Column="1" Margin="0,590,0,0" HorizontalAlignment="Stretch" x:Name="consoleWindow" IsEnabled="True" VerticalAlignment="Stretch"
>
<TextBlock x:Name="myConsoleWindowTextBlock" Text="{Binding Path=consoleText}"/>
</GroupBox>
</Grid>
</Grid>
</Window>
This is the code behind:
using System.Windows;
using System.Runtime.CompilerServices;
using System.ComponentModel;
namespace UIDesigner
{
public partial class UIWindow : Window
{
public UIWindow()
{
InitializeComponent();
}
private string _consoleText;
public string consoleText
{
get{ return _consoleText;}
set
{
_consoleText = value;
NotifyPropertyChanged("consoleText");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
Then in my main class, I call this UIWindow like this:
namespace UIDesigner
{
public partial class Main : Window
{
public Main()
{
InitializeComponent();
}
private void LoginButton_Click_1(object sender, RoutedEventArgs e)
{
var myUIWindow = new UIWindow();
myUIWindow.PropertyChanged += new PropertyChangedEventHandler(UIWindow_PropertyChanged);
myUIWindow.consoleText = "Hello User!";
myUIWindow.ShowDialog();
this.Close();
}
private void LoginButton_MouseEnter_1(object sender, MouseEventArgs e)
{
}
static void UIWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("Something Changed!");
MessageBox.Show(e.PropertyName);
}
}
}
Now I have two problems here:
First, when my UI window starts, I indeed received two message boxes, saying "something changed" followed by "consoleText". So that means the consoleText is changed successfully. But after my UIWindow shows up, the textblock is empty, I cannot see "Hello User!" there. Seems like Text="{Binding Path=consoleText} part is not working correctly in my xaml file.
Second and most importantly, I want to change the consoleText in another different class, namely in DesignerCanvas.Commands.cs. For that I couldn't figure out any solution. I want something like this in my DesignerCanvas.Commands.cs:
namespace UIDesigner
{
public partial class DesignerCanvas
{
private void changeConsoleOutput(string updatedConsoleText)
{
myUIWindow.consoleText = updatedConsoleText; //obviously, this is not working
}
}
}
Any kind of suggestion will be much appreciated.
1.First of two set the value in UI just add below one line
in constructor of UIWindow class
this.DataContext=this;
//because only specifying property consoletext, it will not able to know where to find consoletext.
2.u can find that UIwindow in App.Current.Windows and cast it to UIWindow type and then can
access the property.
foreach(Window win in App.Current.Windows)
{
if (win as UIWindow != null)
{
(win as UIWindow).consoletext = updatedConsoleText;
}
}
For second problem
Change
<TextBlock x:Name="myConsoleWindowTextBlock" Text="{Binding Path=consoleText}"/
To
<TextBlock x:Name="myConsoleWindowTextBlock" Text="{Binding Path=.}"/
and
in UIWindow constructor set
myConsoleWindowTextBlock.Datacontext=consoleText;

Unable to add a new row to the observablecollection listbox?

In my project I have the following pages:
Mainwindow.xaml
Window1.xaml
User.cs
App.xaml
I declared the Name and Age property in User.cs and on MainWindow.xaml I made a ObservableCollection for a ListBox. MainWindow has a Button Add. When we click on the Add button. Then the Window1.xaml form is displayed. Which has two TextBoxes of name and age and a button (ADD Name). Now I want when we click on the Add Name button then the details in the both text boxes are attached to the ObservableCollection that is defined on the Mainwindow.xaml.cs.
Please suggest me want can I do:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void OnInit(object sender, RoutedEventArgs e)
{
string textA=textBox1.Text;
int textB=Convert.ToInt32(textBox2.Text);
this.DataContext = new User(textA, textB);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
User user = (User)(this.DataContext);
new MainWindow().observableCollection.Add(user);
this.Close();
}
}
now I ran this code I am unable to get the values in the user object.
The problem is you are not accessing the MainWindow CbservableCollection, you are creating a new MainWindow.
If this Window1 is a dialog you have a few options
Pass the MainWindow into Window1 as its owner
Use the Window1 as a dialog and fetch the changes when it closes
personally I think the 2nd option is the best, but it depends how you are calling Window1
Example2:
MainWindow Class
public partial class MainWindow : Window
{
private ObservableCollection<User> _myList = new ObservableCollection<User>();
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<User> MyCollection
{
get { return _myList; }
set { _myList = value; }
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
var dialog = new Window1();
if (dialog.ShowDialog() == true)
{
MyCollection.Add(dialog.NewUser);
}
}
}
MainWindow xaml
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="405" Name="UI">
<Grid DataContext="{Binding ElementName=UI}" >
<ListBox ItemsSource="{Binding MyCollection}" DisplayMemberPath="TextA" Margin="0,0,0,47" />
<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="0,0,0,12" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click_1" />
</Grid>
</Window>
Window1 xaml
<Window x:Class="WpfApplication8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="119" Width="300" Name="UI">
<StackPanel DataContext="{Binding ElementName=UI}">
<TextBox Text="{Binding NewUser.TextA}" />
<TextBox Text="{Binding NewUser.TextB}" />
<Button Click="button1_Click" HorizontalAlignment="Right" Width="90" Height="30" Content="Add" />
</StackPanel>
</Window>
Window1 code
public partial class Window1 : Window, INotifyPropertyChanged
{
private User _newUser = new User();
public Window1()
{
InitializeComponent();
}
public User NewUser
{
get { return _newUser; }
set { _newUser = value; NotifyPropertyChanged("NewUser"); }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
User Class
public class User : INotifyPropertyChanged
{
private string _textA;
private string _textB;
public string TextA
{
get { return _textA; }
set { _textA = value; NotifyPropertyChanged("TextA"); }
}
public string TextB
{
get { return _textB; }
set { _textB = value; NotifyPropertyChanged("TextB"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}

Data Binding between two TextBoxes in different windows

I have created a program that changes the name in a TextBox when check or unckeck a Checkbox. I want to replicate this Textbox in a different window. I thought with Data Mining in the xaml would be possible, but the name only appears in one window. Second window window does´t recieve the data. I show you the code of the two windows.
Can you help me? Thankss
Window 1.cs---
namespace WpfApplication1
{
public partial class Window1 : Window
{
Texto prueba = new Texto("Carlos");
public static string s;
public Window1()
{
InitializeComponent( );
// Fill initial person fields
this.textBox1.Text = prueba.Name;
}
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
prueba.Name="Carlos";
textBox1.DataContext = prueba;
textBox1.Text = prueba.Name;
}
private void checkBox1_UnChecked(object sender, RoutedEventArgs e)
{
prueba.Name = "Luis";
textBox1.DataContext = prueba;
textBox1.Text = prueba.Name;
}
}
public class Texto
{
string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Texto( ) {}
public Texto(string name)
{
this.name = name;
}
}
}
window1 xaml-------
<Grid>
<CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="62,118,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_UnChecked" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="44,140,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>
window2 cs-----
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Window1 nueva = new Window1();
nueva.Show();
}
}
}
window2 xaml--------
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="82,121,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox DataContext="prueba" Text="{Binding Path=Name}" Height="23" HorizontalAlignment="Left" Margin="57,84,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
</Grid>
You will have to pass a reference to either the first window or the object that you're updating the text property on to the second window, it's DataContext property will do for that, you can then bind the second windows controls to it.
In this demo application I've created a MainWindow and a second window (Window1), the application starts in Main window like this.
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public string TestString
{
get { return (string)GetValue(TestStringProperty); }
set { SetValue(TestStringProperty, value); }
}
public static readonly DependencyProperty TestStringProperty = DependencyProperty.Register("TestString", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
// setup the test string.
TestString = "this is a test.";
// Create the second window and pass this window as it's data context.
Window1 newWindow = new Window1()
{
DataContext = this
};
newWindow.Show();
}
}
MainWindow.xaml - Take note of the DataContext line in the Window declaration.
<Window x:Class="WpfApplication5.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"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid>
<TextBox Text="{Binding TestString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="91,84,185,189" />
</Grid>
</Window>
Now for Window1 the code behind is just an empty default window class so I won't post it but the xaml is.
Window1.xaml
<Window x:Class="WpfApplication5.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">
<Grid>
<TextBlock Text="{Binding TestString, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
Don't set the DataContext explicitly, or only via another Binding. Your
<TextBox DataContext="prueba"
does nothing helpful. DataContext will be inherited as long as it is not overwritten. Don't set it explicitly. It should be enought to set it once on both windows.
Create your data object in your MainWindow
Texto prueba = new Texto("Carlos");
Window1 nueva = new Window1();
nueva.DataContext = prueba;
nueva.Show();
and remove all the other DataContext assignments.
A couple of things wrong here, but I can probably give you a quick solution to fix your problem. First off your DataContext on window 2 isn't working properly, you can set it exclusively in your code right before you show window1...
private void button1_Click(object sender, RoutedEventArgs e)
{
Window1 nueva = new Window1();
this.DataContext = nueva.prueba;
nueva.Show();
}
Next you have to fire INotifyPropertyChanged in your Texto class...
public class Texto : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string name;
public string Name
{
get { return this.name; }
set
{
this.name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public Texto( ) {}
public Texto(string name)
{
this.name = name;
}
}
If both textboxes share a common datacontext it will "just work" without any code required...

Categories