【问题标题】:TabItem UIElement OnPropertyChangedTabItem UIElement OnPropertyChanged
【发布时间】:2016-04-05 13:09:51
【问题描述】:

我有一个TabControl 和一个SelectionChanged 事件。 When the selected TabPage changes, I want to get notified for the selected TabPage if a value of one of the UIElements on the TabPage has changed.

private FrameworkElement CurrentFrameworkElement { get; set; }

public TabEvents(DispatcherEvents dispatcherEvents)
    : base(dispatcherEvents)
{
    EventManager.RegisterClassHandler(typeof(System.Windows.Controls.TabControl), System.Windows.Controls.TabControl.SelectionChangedEvent, new SelectionChangedEventHandler(TabControl_SelectionChanged), true);
}

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.Source is System.Windows.Controls.TabControl)
    {
        var ti = ((System.Windows.Controls.TabControl)e.Source).SelectedItem as TabItem;
        CurrentFrameworkElement = e.Source as System.Windows.Controls.TabControl;
    }
}

使用此代码,我可以获得当前的TabItem。如何检测当前TabItem 内的 UIElement 值的变化?例如在TextBox 中输入文本或切换CheckBox 应该会发出通知。

我找到了 ObservableUIElementCollection here 的实现,但我不知道我是否可以在这种情况下使用它以及如何使用它。

【问题讨论】:

  • 要更改文本框、复选框等内容,您必须将 text 属性或 ischecked 属性绑定到窗口中的属性,您可以在网上找到许多关于如何使用 WPF 的教程,解释如何使用去做
  • @GlenThomas 是的,我可以访问 View / ViewModel
  • 看看基本的 WPF 概念,如数据绑定与 MVVM 模式的结合以及如何使用它们。也许像this 这样的东西会让它更容易开始。
  • MVVM?监听对应TabItem ViewModel 的INotifyPropertyChanged 事件。顺便说一句,通知基本上是 ViewModels 通信,视图中绝对不需要逻辑。

标签: c# wpf


【解决方案1】:

您可以在 ViewModel 中跟踪您的更改。我通过从属性设置器中标记一个字段来做了类似的事情:

bool _hasChanged = false;

public string Name
{
    get
    {
        return _name;
    }

    set
    {
        if (value != _name)
        {
            _name = value;
            _hasChanged = true;
        }
    }
}

然后,当您的选项卡更改时,检查 _hasChanged 字段的值

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多