C# 我想绑定一个类';“我的用户”控件中控件的属性

C# 我想绑定一个类';“我的用户”控件中控件的属性,c#,wpf,binding,wpf-controls,C#,Wpf,Binding,Wpf Controls,我正在努力找到一个解决我的绑定问题的方法 我有一个用户控件,它有一个按钮用于调用一个单独的窗口,用户可以在其中选择一个对象。选择此对象后,窗口将关闭,用户控件中的对象将根据选择更新其属性。 此对象的属性绑定到用户控件中的控件,但当我更新对象中的属性时,控件中的值不会更新(我希望这是有意义的) 下面是一个精简后的代码: public partial class DrawingInsertControl : UserControl { private MailAttachment Attac

我正在努力找到一个解决我的绑定问题的方法

我有一个用户控件,它有一个按钮用于调用一个单独的窗口,用户可以在其中选择一个对象。选择此对象后,窗口将关闭,用户控件中的对象将根据选择更新其属性。 此对象的属性绑定到用户控件中的控件,但当我更新对象中的属性时,控件中的值不会更新(我希望这是有意义的)

下面是一个精简后的代码:

public partial class DrawingInsertControl : UserControl
{
    private MailAttachment Attachment { get; set; }        

    public DrawingInsertControl(MailAttachment pAttachment)
    {
        Attachment = pAttachment;

        InitializeComponent();

        this.DataContext = Attachment;
    }

    private void btnViewRegister_Click(object sender, RoutedEventArgs e)
    {
        DocumentRegisterWindow win = new DocumentRegisterWindow();
        win.ShowDialog();

        if (win.SelectedDrawing != null)
        {
            Attachment.DwgNo = win.SelectedDrawing.DwgNo;
            Attachment.DwgTitle = win.SelectedDrawing.Title;
        }
    }
}
以及xaml:

<UserControl x:Class="DrawingInsertControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="310" d:DesignWidth="800" >
<Border BorderBrush="Black" BorderThickness="2" Margin="10">
    <Grid>
我省略了名称空间和其他我认为不相关的东西。
提前感谢您的帮助。

您的
邮件附件
类应实现接口:

public class MailAttachment: INotifyPropertyChanged
{

    private string dwgNo;
    public string DwgNo{
        get { return dwgNo; }
        set
        {
            dwgNo=value;
            // Call NotifyPropertyChanged when the property is updated
            NotifyPropertyChanged("DwgNo");
        }
    }

  // Declare the PropertyChanged event
  public event PropertyChangedEventHandler PropertyChanged;

  // NotifyPropertyChanged will raise the PropertyChanged event passing the
  // source property that is being updated.
  public void NotifyPropertyChanged(string propertyName)
  {
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
  }  
}
这将强制控件观察
PropertyChanged
事件。因此,您的控件可以收到有关更改的通知


我提供的代码在C#上,但是,我希望您能将其翻译成VB.Net。

Epic bro,感谢您快速简洁的回复,效果非常好!
Public Class MailAttachment
    Public Property DwgNo As String
End Class
public class MailAttachment: INotifyPropertyChanged
{

    private string dwgNo;
    public string DwgNo{
        get { return dwgNo; }
        set
        {
            dwgNo=value;
            // Call NotifyPropertyChanged when the property is updated
            NotifyPropertyChanged("DwgNo");
        }
    }

  // Declare the PropertyChanged event
  public event PropertyChangedEventHandler PropertyChanged;

  // NotifyPropertyChanged will raise the PropertyChanged event passing the
  // source property that is being updated.
  public void NotifyPropertyChanged(string propertyName)
  {
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
  }  
}