Reputation: 69
This is my model
public class PersonModel : INotifyPropertyChanged
{
private string _firstname;
public string FirstName
{
get {return _firstname; }
set { _firstname = value; RaisePropertyChanged("FirstName"); }
}
private string _lastname;
public string LastName
{
get { return _lastname; }
set { _lastname = value; RaisePropertyChanged("LastName"); }
}
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
I have a view basically a user control
<UserControl x:Class="WpfApplication1.UserControl1"
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:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Visibility="{Binding HideStatus, Mode=TwoWay}">
<UserControl.DataContext>
<local:PersonViewModel></local:PersonViewModel>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="117*"></RowDefinition>
<RowDefinition Height="33*"></RowDefinition>
<RowDefinition Height="75*"></RowDefinition>
<RowDefinition Height="75*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
<TextBox Text="{Binding FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,67" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
<TextBlock Text="FirstName" Margin="0,71,0,0" Grid.RowSpan="2"></TextBlock>
<TextBox Text="{Binding LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,90,0,12" Background="#FFF0F0F0" DataContext="{Binding Path=Person}" Grid.RowSpan="2" />
<Button Grid.Row="3" Content="Button" Height="29" HorizontalAlignment="Left" Margin="12,23,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding UpdateView}" />
</Grid>
In Main window which has one button "Update From Main UI" and the usercontrol which we have created already
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525"
xmlns:model="clr-namespace:WpfApplication1"
>
<Grid>
<Grid.DataContext>
<local:PersonViewModel/>
</Grid.DataContext>
<local:UserControl1 x:Name="control1" />
<Button Content="Update From Main UI" Height="23" Command="{Binding UpdateView}" HorizontalAlignment="Left" Margin="358,276,0,0" Name="button1" VerticalAlignment="Top" Width="145" />
</Grid>
The problem is if i click on the button inside the usercontrol its refreshing the changes, but the same thing is not happening when i tried to click the button from main window. What is the problem?
Upvotes: 0
Views: 2991
Reputation:
The DataContext
of your main Window is another class instance of PersonViewModel
than the DataContext
of your UserControl. Therefore, executing UpdateView
from the main window does not update your UserControl.
You should write this in your main window:
<Button Command="{Binding Path=DataContext.UpdateView, ElementName=control1}" ... />
Via ElementName
, you tell the Binding to refer to the user control. Via DataContext.UpdateView
, you are refering to the user control's UpdateView
command.
Upvotes: 1