绑定问题
XAML
<TextBlock Grid.Column="1"
Height="37"
Margin="8,17,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="20"
Text="{Binding Contact.Name,
UpdateSourceTrigger=PropertyChanged}" />
C# 背后的代码 XAML
public partial class Conversation : Window
{
private Friend _Contact;
public Friend Contact
{
get
{
return _Contact;
}
set
{
_Contact = value;
OnPropertyChanged ( "Contact" );
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged ( string propName )
{
if ( this . PropertyChanged != null )
this . PropertyChanged (
this , new PropertyChangedEventArgs ( propName ) );
}
#endregion
public Conversation ( Friend _Friend )
{
InitializeComponent ( );
Contact = _Friend;
}
.
.
.
}
C# Friend 类
public class Friend : Person
{
.
.
.
}
C# Person 类
public class Person : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged ( "Name" );
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged ( string propName )
{
if ( this . PropertyChanged != null )
this . PropertyChanged ( this , new PropertyChangedEventArgs ( propName ) );
}
#endregion
.
.
.
}
My问题:为什么绑定不起作用?
XAML
<TextBlock Grid.Column="1"
Height="37"
Margin="8,17,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="20"
Text="{Binding Contact.Name,
UpdateSourceTrigger=PropertyChanged}" />
C# Code behind XAML
public partial class Conversation : Window
{
private Friend _Contact;
public Friend Contact
{
get
{
return _Contact;
}
set
{
_Contact = value;
OnPropertyChanged ( "Contact" );
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged ( string propName )
{
if ( this . PropertyChanged != null )
this . PropertyChanged (
this , new PropertyChangedEventArgs ( propName ) );
}
#endregion
public Conversation ( Friend _Friend )
{
InitializeComponent ( );
Contact = _Friend;
}
.
.
.
}
C# Friend Class
public class Friend : Person
{
.
.
.
}
C# Person Class
public class Person : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged ( "Name" );
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged ( string propName )
{
if ( this . PropertyChanged != null )
this . PropertyChanged ( this , new PropertyChangedEventArgs ( propName ) );
}
#endregion
.
.
.
}
My Question : Why Binding Doesn't Work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
联系人需要是一个属性而不是一个字段。
另外,您需要更改绑定,以便源是窗口类。
Contact needs to be a property rather than a field.
Also, you need to change the binding so that the source is the window class.
正如旺伯格所说,联系必须是一种财产。
正如 wangberger 所暗示的那样,您没有将绑定目标(TextBlock 控件)或其任何祖先(例如窗口)的 DataContext 设置为绑定源(在您的情况下为窗口本身)。
另外,请阅读 Microsoft 的有关命名约定的 .NET 指南。
PS Setter 应该仅在 value != _name 时引发 PropertyChanged;
As wangberger stated, contact must be a property.
As wangberger implied, you did not set the DataContext of the binding target (TextBlock control) or any of its ancestors (e.g. the window) to the binding source (in your case the window itself).
Also, please read Microsoft's .NET Guidelines regarding naming conventions.
P.S. Setter should only raise PropertyChanged if value != _name;