如何在XAML中将datacontext设置为用户控件的类?

时间:2013-10-08 13:38:37

标签: c# wpf xaml user-controls

我有以下类实现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));
      }
  }
}

MainWindow.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>

但文字框未在Text="{Binding Path=GASpreviousUpdate, Mode=OneWay}"Text="{Binding Path=GASnextUpdate, Mode=OneWay}"

的用户界面中更新

我认为我需要设置datacontext但不确定如何在usercontrol XAML中执行此操作。请有人指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

尝试:

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

我不会调用字段properties