이것은 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";
}
"때 쇼하여 PropertyChanged". 이것은 무엇을 의미 하는가? 너에게 무엇을 줄까요? 이 코드에서 null 일 수있는 유일한 것은'PropertyChanged'입니다. 'iCounter'는'int'이므로 null이 될 수 없습니다. 'PropertyChanged'가 null 인 경우 아무 것도 바인딩되지 않았으므로 아무도 듣지 않습니다. –
아참. iCounter의 속성 설정자에서 원하는 것은'_iCounter = value;'가 아니라'value = _iCounter; '입니다. –
어떻게 이벤트를 등록하지 않았습니까? –