2

I'm struggling to find a solution to my binding issue.

I have a User Control, which has a button for calling a separate window, in which the user can select an object. Upon selecting this object the window closes and an object in the user control has it's properties updated according to the selection. The properties of this object are bound to controls in the user control, but when I update the properties in the object, the values in the controls are not updated (I hope that makes sense).

here is a slimmed down code behind:

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;
        }
    }
}

and the 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>

...

<TextBox Grid.Column="1" Name="txtDocNo" Text="{Binding DwgNo}" />

and finally the attached object which is in a separate module:

Public Class MailAttachment
    Public Property DwgNo As String
End Class

I've omitted namespaces and other stuff I didn't see as relevant. Thanks in advance for any help.

1 Answer 1

2

Your MailAttachment class should implement INotifyPropertyChanged Interface:

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));
     }
  }  
}

This will force your control to observe PropertyChanged event. So your control can be notified about changes.

The code I provided is on C#, but, I hope you can translate it to VB.Net.

1
  • Epic bro, thanks for the swift and concise reply, worked perfectly!
    – RazorUK84
    Jan 29, 2013 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.