0
简单来说,我有一个棱镜模块有几个视图。如何激活视图时获取?
其中之一是一个带有网格的用户控件,我需要获取这个视图是否活动来重新加载它的datacontext。
我知道我必须使用IActiveAware
接口,但我无法弄清楚如何...
编辑:
MyUserControl.xaml
<UserControl x:Class="[...]
mc:Ignorable="d">
<UserControl.Resources>
<dc:MyViewModel x:Key="MyViewModel" />
</UserControl.Resources>
<Grid DataContext="{StaticResource MyViewModel}">
<Grid.RowDefinitions>
[...]
MyUserControl.xaml .cs
public partial class MyUserControl: UserControl, IActiveAware
{
public MyUserControl(IRegionManager _regionManager)
{
regionManager = _regionManager;
InitializeComponent();
}
public event EventHandler<EventArgs> Activated;
public event EventHandler<EventArgs> Deactivated;
private bool _IsActive;
public bool IsActive
{
get
{
return _IsActive;
}
set
{
_IsActive = value;
IActiveAware vmAware = DataContext as IActiveAware;
if (vmAware != null)
vmAware.IsActive = value;
}
}
public event EventHandler IsActiveChanged = delegate { };
[...]
MyViewModel.cs
public class MyViewModel: IActiveAware
{
private bool isActive { get; set; }
public bool IsActive
{
get
{
return isActive;
}
set
{
isActive = value;
LoadData();
IsActiveChanged(this, EventArgs.Empty);
}
}
public event EventHandler IsActiveChanged = delegate { };
[...]
谢谢,我跟着这个例子,但当我改变视图什么都没有触发......我在问题中添加了我的代码 – Doc