0

我有以下实现 INotifyPropertyChanged 的​​类

更新通知.cs

public class updateNotify : INotifyPropertyChanged
{

  private DateTime? previousUpdate;
  private DateTime? nextUpdate;

  public event PropertyChangedEventHandler PropertyChanged;

  public updateNotify()
  {
  }

  public updateNotify(DateTime? value)
  {
      this.previousUpdate = value;
  }

  public DateTime? GASpreviousUpdate
  {
      get { return previousUpdate; }
      set
      {
          previousUpdate = value;
          // Call OnPropertyChanged whenever the property is updated
          OnPropertyChanged("GASpreviousUpdate");
      }
  }

  public DateTime? GASnextUpdate
  {
      get { return nextUpdate; }
      set
      {
          nextUpdate = value;
          // Call OnPropertyChanged whenever the property is updated
          OnPropertyChanged("GASnextUpdate");
      }
  }

  // Create the OnPropertyChanged method to raise the event 
  protected void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }
}

主窗口.xaml.cs

    public partial class MainWindow 
    {
        private updateNotify properties = new updateNotify();

'''

        public void start_dispatcherTimer()
        {
            dispatcherTimer.Stop();
            DateTime timeNow = DateTime.Now;
            properties.GASpreviousUpdate = timeNow;
            properties.GASnextUpdate = timeNow.AddMinutes((double)Properties.Settings.Default.GASInterval);
            dispatcherTimer.Start();
        }

    }

然后在 XAML

<UserControl x:Class="ProjectXYZ.Content.MainData"
             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:DesignWidth="300" Height="400" >
....
                <StackPanel>
                    <Label Content="Previous Refresh" Target="{Binding ElementName=PreviousRefresh}"/>
                    <TextBox x:Name="PreviousRefresh" Width="140" Text="{Binding Path=GASpreviousUpdate, Mode=OneWay}"/>
                </StackPanel>
                <StackPanel>
                    <Label Content="Next Refresh" Target="{Binding ElementName=NextRefresh}"/>
                    <TextBox x:Name="NextRefresh" Width="140" Text="{Binding Path=GASnextUpdate, Mode=OneWay}"/>
                </StackPanel>

</UserControl>

但文本框没有在 UI 中Text="{Binding Path=GASpreviousUpdate, Mode=OneWay}"更新Text="{Binding Path=GASnextUpdate, Mode=OneWay}"

我想我需要设置数据上下文,但不确定如何在用户控件 XAML 中执行此操作。请有人能指出我正确的方向吗?

4

1 回答 1

2

尝试:

public partial class MainWindow 
{
    public MainWindow()
    {
        this.DataContext = this.properties; // <----
    }
}

我不会打电话给现场properties

于 2013-10-08T13:45:36.400 回答