2010-12-27 4 views
1

작동하지 않음에서 INotifyPropertyChanged 그러나 그것은 나에게 널을 줄 것이다 때 쇼하여 PropertyChanged 그래서 난 내 코드는 다음과 같다 .. ..내가에서 INotifyPropertyChanged를 사용하고

public class Entities : INotifyPropertyChanged 
{ 

    public Entities(int iCount) 
    { 
     _iCounter = iCount; 
    } 


    private int _iCounter; 
    public int iCounter 
    { 
     get 
     { 
      return _iCounter; 
     } 
     set 
     { 
      value = _iCounter; 
      NotifyPropertyChanged("iCounter"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

감사합니다 ... 무엇을 할 수 있는지

+1

"때 쇼하여 PropertyChanged". 이것은 무엇을 의미 하는가? 너에게 무엇을 줄까요? 이 코드에서 null 일 수있는 유일한 것은'PropertyChanged'입니다. 'iCounter'는'int'이므로 null이 될 수 없습니다. 'PropertyChanged'가 null 인 경우 아무 것도 바인딩되지 않았으므로 아무도 듣지 않습니다. –

+7

아참. iCounter의 속성 설정자에서 원하는 것은'_iCounter = value;'가 아니라'value = _iCounter; '입니다. –

+0

어떻게 이벤트를 등록하지 않았습니까? –

답변

-1

이것은 INotifyPropertyChanged의 버그라고 생각합니다.

LABLE 같은 UI 컨트롤이 해결

1 해결

1 할당 iCounter 속성을있을 수 있습니다.

2 이번에는 속성 값을 변경하십시오. PropertyChanged 이벤트는 메서드에 대한 참조를 가지며 null이 아닙니다. 엔터티 클래스 생성자 내가

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <Grid.Resources> 
      <ToolTip x:Key="@tooltip"> 
       <TextBlock Text="{Binding CompanyName}"/> 
     </ToolTip> 
     </Grid.Resources> 

     <TextBlock Text="{Binding Name}" Background="LightCoral" /> 
    <Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="{DynamicResource @tooltip}" Grid.Row="1"/> 
    <Button Click="Button_Click" Grid.Row="2" Margin="20">Click Me</Button> 
</Grid> 

WPF

에서 데모 코드를 제공하고

에서

2 해결

지정하여 PropertyChanged 대표는 회사 이름이 툴팁에 할당 여기를 참조하십시오.

//이 Window1.Cs가 공개 번째 윈도를() 파일 { 의 DataContext DemoCustomer.CreateNewCustomer =(); InitializeComponent(); 값을 변경 }

// 이제 DemoCustomer 클래스

public class DemoCustomer : INotifyPropertyChanged 
{ 
    // These fields hold the values for the public properties. 
    private Guid idValue = Guid.NewGuid(); 
    private string customerName = String.Empty; 
    private string companyNameValue = String.Empty; 
    private string phoneNumberValue = String.Empty; 

    public event PropertyChangedEventHandler PropertyChanged; 



    protected void OnPropertyChanged(string propertyName) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    // The constructor is private to enforce the factory pattern. 
    private DemoCustomer() 
    { 
     customerName = "no data"; 
     companyNameValue = "no data"; 
     phoneNumberValue = "no data"; 
    } 

    // This is the public factory method. 
    public static DemoCustomer CreateNewCustomer() 
    { 
     return new DemoCustomer(); 
    } 

    // This property represents an ID, suitable 
    // for use as a primary key in a database. 
    public Guid ID 
    { 
     get 
     { 
      return this.idValue; 
     } 
    } 

    public string CompanyName 
    { 
     get { return this.companyNameValue; } 

     set 
     { 
      if (value != this.companyNameValue) 
      { 
       this.companyNameValue = value; 
       OnPropertyChanged("CompanyName"); 
      } 
     } 
    } 
    public string PhoneNumber 
    { 
     get { return this.phoneNumberValue; } 

     set 
     { 
      if (value != this.phoneNumberValue) 
      { 
       this.phoneNumberValue = value; 
       OnPropertyChanged("PhoneNumber"); 
      } 
     } 
    } 
} 

마지막

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     DemoCustomer dc = this.DataContext as DemoCustomer; 

     dc.CompanyName = "Temp"; 


    } 
0

내 프로그램에 코드를 넣으려고했는데 정상적으로 작동합니다. EventArg를 속성으로 가져오고 있습니다.

class Program 
    { 
     static void Main(string[] args) 
     { 
      var ent = new Entities(10); 
      ent.PropertyChanged += new PropertyChangedEventHandler(ent_PropertyChanged); 
      ent.iCounter = 100; 
     } 

     static void ent_PropertyChanged(object sender, PropertyChangedEventArgs e) 
     { 
      throw new NotImplementedException(); 
     } 
    } 


    public class Entities : INotifyPropertyChanged 
    { 

     public Entities(int iCount) 
     { 
      _iCounter = iCount; 
     } 
     private int _iCounter; 
     public int iCounter 
     { 
      get 
      { 
       return _iCounter; 
      } 
      set 
      { 
       _iCounter = value; 
       NotifyPropertyChanged("iCounter"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 
    } 

정확한 오류 정보는 무엇입니까?

+0

이것은 INotifyPropertyChanged 인터페이스의 유효한 사용이 아닙니다. – TalentTuner

+1

왜 이것이 아닌가요? INotifyPropertyChanged 인터페이스의 유효한 사용. 이것은 ObservableCollection에서 많이 사용됩니다. ObservableCollection에서는 데이터 소스가 변경 될 때 구독 컨트롤이 업데이트되고 모든 컨트롤에 대해 DataContext를 설정할 수 있습니다. MSDN에서 - "INotifyPropertyChanged 인터페이스는 클라이언트 (일반적으로 바인딩 클라이언트)에게 속성 값이 변경되었음을 알리는 데 사용됩니다." 나는 당신의 대답에 다르게 사용되는 것을 보지 못합니다. – Bhaskar

관련 문제