마지막으로 Silverlight MVVM 예제를 사용하여 이름과 성 텍스트 상자의 값을 변경하면 전체 이름이 자동으로 변경됩니다. 내가2 자 이상 중 첫 번째 또는 마지막 이름의을 변경하는 경우INotifyPropertyChanged는 왜 적어도 두 개의 문자가 변경 될 때만 발생합니까?
그러나, 이상하게, 에서 INotifyPropertyChanged에서 상속 내 모델은 통지됩니다. 나는 어떤 이벤트가
- "스미스"에서 "Smith12는"그 사건은 해고
Silverlight/XAML/INotifyPropertyChanged에서 이전에이 프로그램을 실행 한 사람이 있습니까? 뭐가 될수 있었는지? "변경됨"으로 알리기 전에 텍스트 상자의 어느 정도를 변경해야하는지 나타내는 어딘가 설정이 있습니까?
Customer.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace TestMvvm345.Model
{
public class Customer : INotifyPropertyChanged
{
public int ID { get; set; }
public int NumberOfContracts { get; set; }
private string firstName;
private string lastName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
public string LastName
{
get { return lastName; }
set
{
lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
public string FullName
{
get { return firstName + " " + lastName; }
}
#region INotify
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
#endregion
}
}
CustomerHeaderView.xaml :
<UserControl x:Class="TestMvvm345.Views.CustomerHeaderView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel HorizontalAlignment="Left">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="FirstName"
Text="{Binding Path=FirstName, Mode=TwoWay}"
Width="150"
Margin="3 5 3 5"/>
<TextBox x:Name="LastName"
Text="{Binding Path=LastName, Mode=TwoWay}"
Width="150"
Margin="0 5 3 5"/>
<TextBlock x:Name="FullName"
Text="{Binding Path=FullName, Mode=TwoWay}"
Margin="0 5 3 5"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</UserControl>
CustomerViewModel 여기
내가 사용하고 코드의 주요 부분이다 .cs :
using System.ComponentModel;
using System.Collections.ObjectModel;
using TestMvvm345.Model;
namespace TestMvvm345
{
public class CustomerViewModel
{
public ObservableCollection<Customer> Customers { get; set; }
public void LoadCustomers()
{
ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
//this is where you would actually call your service
customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 22 });
customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 33 });
customers.Add(new Customer { FirstName = "Robert", LastName = "Smith", NumberOfContracts = 2 });
customers.Add(new Customer { FirstName = "Hank", LastName = "Jobs", NumberOfContracts = 5 });
Customers = customers;
}
}
}
MainPage.xaml.cs를 :
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
CustomerViewModel customerViewModel = new CustomerViewModel();
customerViewModel.LoadCustomers();
CustomerHeaderView.DataContext = customerViewModel.Customers;
}
UPDATE :
내가 WPF에서이 프로젝트를 리메이크하고 잘 작동. 아마도 Silverlight 3 문제 일 것입니다.
없음 문제가 있는지 무엇을 : 여기 (현재 답이없는) 내 버그 리포트입니다. 그런데 FullName Mode = TwoWay에 대한 설정자가 없기 때문에 작동하지 않습니다. 당신이 몰랐을 경우에. –
맞습니다. FullName은 Oneway 일뿐입니다. 그러나 "Smith"를 "Smith12"로 변경하고 텍스트 상자에서 탭을 열면 fullname이 변경됩니다. 그러나 "Smith"를 "Smith1"로 변경하면 아무 것도 변경되지 않습니다. 문제는 : 적어도 두 개의 문자가 변경된 경우에만 텍스트 상자가 변경되는 이유는 무엇입니까? –