WPF TabControl Page added, close, mass participation, refresh

Original: WPF add the TabControl Page, close, mass participation, refresh

A, tabcontrol


  
  
  1. <TabControl Grid.Row= "0" x:Name= "tbc_main" ItemsSource= "{Binding DataList,Mode=OneWay}" SelectedIndex= "{Binding TabSelectedIndex}">
  2. <TabControl.ItemTemplate>
  3. <DataTemplate>
  4. <StackPanel Orientation= "Horizontal" Margin= "0" Height= "28">
  5. <TextBlock Text= "{Binding Header}" Margin= "10,7,0,0"/>
  6. <Button x:Name= "btn_close" Content= "×" BorderBrush= "Transparent" Width= "16" Cursor= "Hand" Margin= "10,0,0,0"
  7. Background= "{x:Null}" Command= "{Binding DataContext.CloseTabCommand,ElementName=tbc_main}" CommandParameter= "{Binding ElementName=btn_close}"
  8. Padding= "0" >
  9. </Button>
  10. </StackPanel>
  11. </DataTemplate>
  12. </TabControl.ItemTemplate>
  13. <TabControl.ContentTemplate>
  14. <DataTemplate>
  15. <ContentControl Content= "{Binding Content}"></ContentControl>
  16. </DataTemplate>
  17. </TabControl.ContentTemplate>
  18. </TabControl>

First, the new


  
  
  1. /// <summary>
  2. /// 新增页面命令:传递参数
  3. /// </summary>
  4. public RelayCommand< string> AddTabItemCommand =>
  5. new Lazy<RelayCommand< string>>(() =>
  6. new RelayCommand< string>(AddTabItem)).Value;
  7. /// <summary>
  8. /// 添加新页面
  9. /// </summary>
  10. /// <param name="param">菜单关联新页面参数</param>
  11. private void AddTabItem(string param)
  12. {
  13. JObject obj = (JObject)JsonConvert.DeserializeObject(param);
  14. string header = obj[ "header"].ToString();
  15. string url = obj[ "url"].ToString();
  16. Boolean isNew = true;
  17. int i = 0;
  18. for (i = 0; i < DataList.Count; i++)
  19. {
  20. if ( string.Equals(DataList[i].Header, header))
  21. {
  22. isNew = false;
  23. break;
  24. }
  25. }
  26. if (isNew)
  27. {
  28. Frame fm = new Frame();
  29. fm.Source = new Uri(url, UriKind.Relative);
  30. DataList.Add( new TabControlModel
  31. {
  32. Header = header,
  33. Content = fm
  34. });
  35. TabSelectedIndex = DataList.ToArray().Length - 1;
  36. }
  37. else
  38. {
  39. TabSelectedIndex = i;
  40. return;
  41. }
  42. }

Second, close the tab


  
  
  1. /// <summary>
  2. /// 关闭tab
  3. /// </summary>
  4. public RelayCommand<Button> CloseTabCommand =>
  5. new Lazy<RelayCommand<Button>>(() =>
  6. new RelayCommand<Button>(CloseTab)).Value;
  7. public void CloseTab(Button btn)
  8. {
  9. TabItem tbc = FindParentTabControl(btn);
  10. foreach (TabControlModel item in DataList)
  11. {
  12. if (item.Equals(tbc.Content))
  13. {
  14. DataList.Remove(item);
  15. break;
  16. }
  17. }
  18. }
  19. /// <summary>
  20. /// 递归找父级TabControl
  21. /// </summary>
  22. /// <param name="reference">依赖对象</param>
  23. /// <returns>TabControl</returns>
  24. private TabItem FindParentTabControl(DependencyObject reference)
  25. {
  26. DependencyObject dObj = VisualTreeHelper.GetParent(reference);
  27. if (dObj == null)
  28. return null;
  29. if (dObj.GetType() == typeof(TabItem))
  30. return dObj as TabItem;
  31. else
  32. return FindParentTabControl(dObj);
  33. }

Third, refresh

viewmodel transmission parameters and increase the time ViewModelLocator


  
  
  1. public PIndexViewModel(string dateTime)
  2. {
  3. MenuList = GetMenuList();
  4. MenuSelectedIndex = 0;
  5. DataList = GetTabControlDataList();
  6. //接收其他页面传递的消息,第二个参数为消息key,控制接收对象
  7. Messenger.Default.Register<String>( this, "AddTab", ReceiveInfo);
  8. DateShow();
  9. timer = new DispatcherTimer();
  10. timer.Interval = TimeSpan.FromSeconds( 1);
  11. timer.Tick += new EventHandler(TimerTick);
  12. timer.Start();
  13. }

  
  
  1. SimpleIoc. Default.Register(() => new PIndexViewModel(DateTime. Now.ToString( "HH:mm:ss")));
  2. public PIndexViewModel PIView
  3. {
  4. get
  5. {
  6. return ServiceLocator.Current.GetInstance<PIndexViewModel>(DateTime. Now.ToString( "HH:mm:ss"));
  7. }
  8. }

Fourth, parameter passing

WPF program is a client program, which means that every user-initiated on their own machines WPF programs that are independent of each other, so there is no problem of multiple users simultaneously access.

Form traditional values ​​and methods querystring [] passed by value because the underlying ASP.NET HTTP protocol is stateless protocol, it is necessary (because the Web server does not remember the state of each page) to transfer information between different pages. The WPF application inside each page are running in the same process space, sharing the same memory, so use Application.Current.Properties it.


  
  
  1. private void Jt(string param)
  2. {
  3. //给接谈传递页面参数
  4. Application.Current.Properties[ "pjt"] = param;
  5. SendInfo = "{ \"header\":\"测试\" , \"url\":\"/Pages/PJt.xaml\" }";
  6. //消息传递给PindexModel接收,打开新的页面,第二个参数为消息key,控制接收对象
  7. Messenger.Default.Send<String>(SendInfo, "AddTab");
  8. }

  
  
  1. public PJtViewModel(string dateTime)
  2. {
  3. string param=( string)Application.Current.Properties[ "pjt"];
  4. Model = GetXfInfo(param);
  5. GetXfjPcCount(Model);
  6. //接收其他页面传递的消息,第二个参数为消息key,控制接收对象
  7. Messenger.Default.Register<String>( this, "Xfpc_xtxfsx", ReceiveXfpcXtxfsx);
  8. Messenger.Default.Register<String>( this, "Xfpc_sqyy", ReceiveXfpcSqyy);
  9. Messenger.Default.Register<String>( this, "Xfpc_ccxf", ReceiveXfpcCcxf);
  10. Messenger.Default.Register< string[]>( this, MsgTypes.列表信息控制_Callback, res =>
  11. {
  12. switch (res[ 0])
  13. {
  14. case "GKXX":
  15. Model.Zysshtml = res[ 1];
  16. break;
  17. }
  18. });
  19. }

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12208817.html
Recommended