【发布时间】: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?监听对应
TabItemViewModel 的INotifyPropertyChanged事件。顺便说一句,通知基本上是 ViewModels 通信,视图中绝对不需要逻辑。