简体   繁体   English

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

[英]How do I set the datacontext to a class for a usercontrol in XAML?

I have the following class which implements INotifyPropertyChanged 我有以下实现INotifyPropertyChanged的类

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

    }

then in XAML 然后在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>

but the textboxes are not updating in the UI for Text="{Binding Path=GASpreviousUpdate, Mode=OneWay}" and Text="{Binding Path=GASnextUpdate, Mode=OneWay}" 但是在Text="{Binding Path=GASpreviousUpdate, Mode=OneWay}"Text="{Binding Path=GASnextUpdate, Mode=OneWay}"的用户界面中文本框未更新

I think I need to set the datacontext but not sure how to do this in the usercontrol XAML. 我想我需要设置数据上下文,但不确定如何在用户控件XAML中执行此操作。 Please can someone point me in the right direction? 请有人能指出正确的方向吗?

try: 尝试:

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

I wouldn't call the field properties 我不会调用字段properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM