2

バインディングの問題の解決策を見つけるのに苦労しています。

ユーザーがオブジェクトを選択できる別のウィンドウを呼び出すためのボタンがあるユーザーコントロールがあります。このオブジェクトを選択すると、ウィンドウが閉じ、ユーザー コントロール内のオブジェクトのプロパティが選択に従って更新されます。このオブジェクトのプロパティはユーザー コントロール内のコントロールにバインドされていますが、オブジェクト内のプロパティを更新しても、コントロール内の値は更新されません (意味があることを願っています)。

これはスリム化されたコードビハインドです:

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>

...

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

最後に、別のモジュールにある添付オブジェクト:

Public Class MailAttachment
    Public Property DwgNo As String
End Class

名前空間や、関連性がないと思われるその他のものは省略しました。助けてくれてありがとう。

4

1 に答える 1

2

クラスはインターフェイスMailAttachmentを実装する必要があります。INotifyPropertyChanged

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に変換できることを願っています。

于 2013-01-29T10:36:40.553 に答える