绑定问题

发布于 2024-11-09 00:48:35 字数 2102 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-11-16 00:48:35

联系人需要是一个属性而不是一个字段。

另外,您需要更改绑定,以便源是窗口类。

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.

感情废物 2024-11-16 00:48:35

正如旺伯格所说,联系必须是一种财产。

正如 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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文