C# 如何在XAML中将datacontext设置为usercontrol的类?

C# 如何在XAML中将datacontext设置为usercontrol的类?,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我有以下实现INotifyPropertyChanged的类 updateNotify.cs public class updateNotify : INotifyPropertyChanged { private DateTime? previousUpdate; private DateTime? nextUpdate; public event PropertyChangedEventHandler PropertyChanged; public updateNoti

我有以下实现INotifyPropertyChanged的类

updateNotify.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));
      }
  }
}
    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();
        }

    }
MainWindow.xaml.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));
      }
  }
}
    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>

....
但是
Text=“{Binding Path=GASpreviousUpdate,Mode=OneWay}”和
Text=“{Binding Path=GASnextUpdate,Mode=OneWay}”的用户界面中的文本框没有更新

我想我需要设置datacontext,但不确定如何在usercontrol XAML中进行设置。有人能给我指一下正确的方向吗?

试试:

public partial class MainWindow 
{
    public MainWindow()
    {
        this.DataContext = this.properties; // <----
    }
}
公共部分类主窗口
{
公共主窗口()
{

this.DataContext=this.properties;//太长,未读取。