一、方案
利用viewmodel之间消息传递方式
二、子页面
1、view.xaml
<Button Content="接谈" Command="{Binding DataContext.JtCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
Style="{StaticResource ButtonSuccess}" CommandParameter="{Binding Param}" Cursor="Hand"/>
2、viewmodel
public RelayCommand<string> JtCommand =>
new Lazy<RelayCommand<string>>(() =>
new RelayCommand<string>(Jt)).Value;
private void Jt(string param)
{
SendInfo = param;
//消息传递给PindexModel接收,打开新的页面,第二个参数为消息key,控制接收对象
Messenger.Default.Send<String>(SendInfo, "AddTab");
}
三、主窗体页面
1、viewmodel
public PIndexViewModel()
{
MenuList = GetMenuList();
MenuSelectedIndex = 0;
DataList = GetTabControlDataList();
//接收其他页面传递的消息,第二个参数为消息key,控制接收对象
Messenger.Default.Register<String>(this, "AddTab", ReceiveInfo);
DateShow();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(TimerTick);
timer.Start();
}
/// <summary>
/// 消息处理
/// </summary>
/// <param name="msg"></param>
private void ReceiveInfo(string msg)
{
AddTabItem(msg);
}
2、xaml.cs
public PIndex()
{
InitializeComponent();
//卸载当前(this)对象注册的所有MVVMLight消息
this.Unloaded += (sender, e) => Messenger.Default.Unregister(this);
}