私は最終的にSilverlight MVVMの例を使用して、名字と姓のテキストボックスの値を変更するとフルネームが自動的に変更されるようにしました。私は少なくとも2文字のいずれかの姓または名のを変更した場合INOTifyPropertyChangedは、少なくとも2文字が変更された場合にのみ発生します。
しかし、と妙に、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の設定がないため動作しません。あなたが気づいていない場合に備えて。 –
Right、FullNameはOnewayである必要がありますが、「Smith」を「Smith12」に変更してテキストボックスのタブを変更すると、フルネームが変更されます。しかし、私が "スミス"を "スミス1"に変更すると、何も変わりません。問題は:少なくとも2文字が変更された場合にのみ、テキストボックス登録が変更される理由は何ですか? –