2009-04-02 4 views
1

마지막으로 Silverlight MVVM 예제를 사용하여 이름과 성 텍스트 상자의 값을 변경하면 전체 이름이 자동으로 변경됩니다. 내가2 자 이상 중 첫 번째 또는 마지막 이름의을 변경하는 경우INotifyPropertyChanged는 왜 적어도 두 개의 문자가 변경 될 때만 발생합니까?

그러나, 이상하게, 에서 INotifyPropertyChanged에서 상속 내 모델은 통지됩니다. 나는 어떤 이벤트가

  • 내가 변경하는 경우를 트리거되지 않습니다 다음 "스미스"에서 "Smith1"를 변경하면 예상대로

    • "스미스"에서 "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 문제 일 것입니다.

  • +0

    없음 문제가 있는지 무엇을 : 여기 (현재 답이없는) 내 버그 리포트입니다. 그런데 FullName Mode = TwoWay에 대한 설정자가 없기 때문에 작동하지 않습니다. 당신이 몰랐을 경우에. –

    +0

    맞습니다. FullName은 Oneway 일뿐입니다. 그러나 "Smith"를 "Smith12"로 변경하고 텍스트 상자에서 탭을 열면 fullname이 변경됩니다. 그러나 "Smith"를 "Smith1"로 변경하면 아무 것도 변경되지 않습니다. 문제는 : 적어도 두 개의 문자가 변경된 경우에만 텍스트 상자가 변경되는 이유는 무엇입니까? –

    답변

    2

    예제 코드를 사용하면 완벽하게 작동합니다.

    public string FirstName 
        { 
         get { return firstName; } 
         set 
         { 
          firstName = value; 
          RaisePropertyChanged("FirstName"); 
          RaisePropertyChanged("FullName"); 
         } 
        } 
    

    이어야 그때 하나의 캐릭터 변화에 포커스를 이동하고 NotifyPropertyChanged의 옆 사용량이 결함이 있기 때문에 전체 이름의 업데이트는

    을 발생합니다

    public string FirstName 
        { 
         get { return firstName; } 
         set 
         { 
          if (firstName != value) 
          { 
           firstName = value; 
           RaisePropertyChanged("FirstName"); 
           RaisePropertyChanged("FullName"); 
          } 
         } 
        } 
    

    당신이 원하는 변경 사항이 발생하지 않도록 이벤트가 실행되도록하고 관련 리 바인딩을 피하십시오.

    +0

    잘 알 으면 좋겠다. 나는 실버 라이트 3을 사용하고 있으므로 실버 라이트 2를 가진 다른 머신에서 시도 할 것이다. –

    -1

    또한 'UpdateSourceTrigger = PropertyChanged'를 Binding 문에 포함 할 수 있습니다. 즉

    Text="{Binding Path=FirstName,UpdateSourceTrigger=PropertyChanged}" 
    

    이렇게하면 텍스트 상자를 변경할 때마다 값이 업데이트됩니다.

    +0

    내가 FirstName 또는 LastName으로 입력하면 모든 것이 공백으로 표시됩니다. Silverlight 3 베타 1을 사용하고 있습니다. 아마도이 문제가 Silverlight 2 도구를 다시 설치할 수 없으므로 나중에 다른 컴퓨터에서 확인해보십시오. 감사합니다. –

    +0

    Internet explorer가 나에게 알려줍니다. "잘못된 속성 값 : UpdateSourceTrigger = PropertyChanged" –

    +1

    UpdateSourceTrigger는 WPF 유일한 것입니다 Silverlight 해결 방법은 http://silverlight.net/forums/t/11547.aspx를 참조하십시오. –

    관련 문제