绑定 INotifyPropertyChanged 仍然不起作用

Binding with INotifyPropertyChanged Still Doesn't Work

试了很多东西,还是不行。对两个 TextBlock 的绑定不起作用。使用了与 非常相似的 INotifyPropertyChanged 界面,但无济于事。

代码:

MainWindow.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ClockWatcher" xmlns:System="clr-namespace:System;assembly=mscorlib"
        x:Name="clockWatcherWindow"
        x:Class="ClockWatcher.MainWindow"
        Title="Clock Watcher" Height="554" Width="949"
    KeyDown="KeysDown" Focusable="True" Closing="SaveSession"
    DataContext="{Binding SM, RelativeSource={RelativeSource Self}}">
    <TextBlock x:Name="programStartBlock" Text="{Binding StartTime, BindsDirectlyToSource=True, FallbackValue=Binding sucks so much!!!,  StringFormat=ProgramStarted: \{0\}, TargetNullValue=This thing is null}" Padding="{DynamicResource labelPadding}" FontSize="{DynamicResource fontSize}"/>
    <TextBlock x:Name="totalTimeLabel" Text="{Binding SM.currentSession.TotalTime, StringFormat=Total Time: \{0\}}" Padding="{DynamicResource labelPadding}" FontSize="{DynamicResource fontSize}"/>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    private const string SESSION_FILENAME = "SessionFiles.xml";

    /// <summary>
    /// Represents, during selection mode, which TimeEntry is currently selected.
    /// </summary>

    public SessionManager SM { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        SM = new SessionManager();
        SM.newAddedCommentEvent += currentTimeEntry_newComment;
        SM.timeEntryDeletedEvent += currentTimeEntry_delete;
        SM.commentEntryDeletedEvent += entry_delete;
    }
}

SessionManager.cs:

public class SessionManager : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        [NonSerialized]
        private DateTime _dtStartTime;
        private Session current_session;
        #region Properties

        public DateTime StartTime
        {
            get
            {
                return _dtStartTime;
            }
            private set
            {
                if (_dtStartTime != value)
                {
                    _dtStartTime = value;
                    OnPropertyChanged("StartTime");
                }
            }
        }


 public Session CurrentSession
    {
        get
        {
            return current_session;
        }
        set
        {
            if (current_session != value)
            {
                OnPropertyChanged("CurrentSession");
                current_session = value;
            }
        }
    }
        #endregion

        public SessionManager()
        {
            _dtStartTime = DateTime.Now;
        }

        private void OnPropertyChanged([CallerMemberName] string member_name = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(member_name));
            }
        }
    }

Session.cs:

public class Session : INotifyPropertyChanged
    {
        private TimeSpan total_time;
        public DateTime creationDate { get; private set; }
        public event PropertyChangedEventHandler PropertyChanged;


        public TimeSpan TotalTime
        {
            get
            {
                return total_time;
            }
            set
            {
                if (total_time != value)
                {
                    OnPropertyChanged("TotalTime");
                    total_time = value;
                }
            }
        }

        public Session()
        {
            creationDate = DateTime.Now;
        }

        private void OnPropertyChanged([CallerMemberName] string member_name = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(member_name));
            }
        }
    }
  1. 在第一个 TextBlock 中,只写 StartTime 而不是 SM.StartTime。

  2. 从第一个 TB 中删除 ElementName。

  3. 创建 CurrentSession public 属性,您的 currentSession 现在是私有的。

  4. 在您的 SessionManager 构造函数中,current_session = new Session();

  5. 从 XAML 中删除 DataContext,在 window 构造函数中使用 this.DataContext = SM;

  6. 如果要在XAML、

    中使用DataContext
    <Window.DataContext>
       <local:SessionManager />
    </Window.DataContext>
    

标记正确的答案绝对是更好的方法,但我只是想更详细地解释为什么你发布的内容不起作用。

问题是,当您在 MainWindow.xaml 中编写 DataContext={Binding SM, RelativeSource={RelativeSource Self} 时,绑定在 MainWindow.xaml.cs 构造函数中执行行 SM = new SessionManager(); 之前被评估。

如果您将 SM 的 getter 更改为:

,您可以看到效果
public SessionManager SM
{
    get { return new SessionManager();}
}

这基本上可以确保当 WPF 评估您的绑定时,它将为您的 SM 属性 获取一个实际对象而不是 null。

只是想也许这将有助于下次理解和减少挫败感:)。按照您提出问题的方式,从技术上讲,您需要在 MainWindow class 上实施 INotifyPropertyChanged,这是一个很大的禁忌。