hi,
Here is my Sample Code:
ViewModel:
private string firstName; public string FirstName { get { return firstName; } set { firstName = value; OnPropertyChanged("FirstName"); } }
.................
..................
InfoViewModel:
private string lastName; public string LastName { get { return lastName; } set { lastName = value; OnPropertyChanged("LastName"); } } private string getFirstName; public string GetFirstName { get { return getFirstName; } set { getFirstName = value; OnPropertyChanged("GetFirstName"); } }
MainWindow.Xaml:
ViewModel viewModel = new ViewModel(); public InfoViewModel infoChildView = new InfoViewModel(); Window1 window1 = new Window1(); public MainWindow() { InitializeComponent(); DataContext = viewModel; } private void Button_Click(object sender, RoutedEventArgs e) { if (!viewModel.IsValid()) { MessageBox.Show("Error Message"); } else {window1.DataContext = infoChildView;//To Window1 infoChildView.GetFirstName = viewModel.FirstName; window1.Show(); }
<TextBox Text="{Binding FirstName, ValidatesOnDataErrors=True}" />
<Button Content="Button"Click="Button_Click"/>
Window1.Xaml:
public MainWindow mainWindow; InfoViewModel infoChildView = new InfoViewModel(); public Window1() { InitializeComponent(); } private void Done_Click(object sender, RoutedEventArgs e) { if (!infoChildView.IsValid()) { MessageBox.Show("Error Message"); } else { MessageBox.Show("Good To go"); } }
<TextBlock Text="{Binding GetFirstName}" />
<TextBox Text="{Binding LastName, ValidatesOnDataErrors=True}"/>
<Button Content="Done" Click="Done_Click"/>
I have already made IDataErrorInfo in both view model (ViewModel and InfoViewModel). The mainWindow is fine but my problem is that I cannot see validation error message in window1 when i clicked Done_Click event if textbox is empty. i think i already DataContext in MainWindow called through a window1.
Am i missing DataContext in window1? Any idea?
Thanks