私はモデルを持っています
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;
}
私はビューモデルを持っています:
public class PersonViewModel
{
public ObservableCollection<PersonModel> Person { get; set; }
public PersonViewModel()
{
Person = CookPersonData();
}
internal static ObservableCollection<PersonModel> CookPersonData()
{
ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
persons.Add(new PersonModel{ FirstName="Raj", LastName="kumar" });
return persons;
}
}
ユーザーコントロールは
<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">
<UserControl.DataContext>
<local:PersonViewModel></local:PersonViewModel>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
<TextBox Text="{Binding FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,106" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
<TextBlock Grid.Row="1" Text="FirstName"></TextBlock>
<TextBox Grid.Row="1" Text="{Binding LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,0,0,110" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
</Grid>
</UserControl>
発生していないデータコンテキストを再バインドする必要があります
can you suggest what is the problem?