一、tabcontrol
-
<TabControl Grid.Row="0" x:Name="tbc_main" ItemsSource="{Binding DataList,Mode=OneWay}" SelectedIndex="{Binding TabSelectedIndex}">
-
<TabControl.ItemTemplate>
-
<DataTemplate>
-
<StackPanel Orientation="Horizontal" Margin="0" Height="28">
-
<TextBlock Text="{Binding Header}" Margin="10,7,0,0"/>
-
<Button x:Name="btn_close" Content="×" BorderBrush="Transparent" Width="16" Cursor="Hand" Margin="10,0,0,0"
-
Background="{x:Null}" Command="{Binding DataContext.CloseTabCommand,ElementName=tbc_main}" CommandParameter="{Binding ElementName=btn_close}"
-
-
Padding="0" >
-
</Button>
-
</StackPanel>
-
</DataTemplate>
-
</TabControl.ItemTemplate>
-
<TabControl.ContentTemplate>
-
<DataTemplate>
-
<ContentControl Content="{Binding Content}"></ContentControl>
-
</DataTemplate>
-
</TabControl.ContentTemplate>
-
</TabControl>
一、新增
-
/// <summary>
-
/// 新增页面命令:传递参数
-
/// </summary>
-
public RelayCommand<string> AddTabItemCommand =>
-
new Lazy<RelayCommand<string>>(() =>
-
new RelayCommand<string>(AddTabItem)).Value;
-
-
/// <summary>
-
/// 添加新页面
-
/// </summary>
-
/// <param name="param">菜单关联新页面参数</param>
-
private void AddTabItem(string param)
-
{
-
JObject obj = (JObject)JsonConvert.DeserializeObject(param);
-
string header = obj["header"].ToString();
-
string url = obj["url"].ToString();
-
Boolean isNew = true;
-
int i = 0;
-
for (i = 0; i < DataList.Count; i++)
-
{
-
if (string.Equals(DataList[i].Header, header))
-
{
-
isNew = false;
-
break;
-
}
-
}
-
if (isNew)
-
{
-
Frame fm = new Frame();
-
fm.Source = new Uri(url, UriKind.Relative);
-
DataList.Add(new TabControlModel
-
{
-
Header = header,
-
Content = fm
-
});
-
TabSelectedIndex = DataList.ToArray().Length - 1;
-
}
-
else
-
{
-
-
TabSelectedIndex = i;
-
return;
-
}
-
}
二、关闭tab
-
/// <summary>
-
/// 关闭tab
-
/// </summary>
-
public RelayCommand<Button> CloseTabCommand =>
-
new Lazy<RelayCommand<Button>>(() =>
-
new RelayCommand<Button>(CloseTab)).Value;
-
-
public void CloseTab(Button btn)
-
{
-
TabItem tbc = FindParentTabControl(btn);
-
foreach (TabControlModel item in DataList)
-
{
-
if (item.Equals(tbc.Content))
-
{
-
DataList.Remove(item);
-
break;
-
}
-
}
-
}
-
-
/// <summary>
-
/// 递归找父级TabControl
-
/// </summary>
-
/// <param name="reference">依赖对象</param>
-
/// <returns>TabControl</returns>
-
private TabItem FindParentTabControl(DependencyObject reference)
-
{
-
DependencyObject dObj = VisualTreeHelper.GetParent(reference);
-
if (dObj == null)
-
return null;
-
if (dObj.GetType() == typeof(TabItem))
-
return dObj as TabItem;
-
else
-
return FindParentTabControl(dObj);
-
}
三、刷新
viewmodel和ViewModelLocator增加时间参数传递
-
public PIndexViewModel(string dateTime)
-
{
-
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();
-
}
-
SimpleIoc.Default.Register(() => new PIndexViewModel(DateTime.Now.ToString("HH:mm:ss")));
-
-
public PIndexViewModel PIView
-
{
-
get
-
{
-
return ServiceLocator.Current.GetInstance<PIndexViewModel>(DateTime.Now.ToString("HH:mm:ss"));
-
}
-
}
四、传参
WPF程序是客户端程序,也就是说每个用户在自己的机器上启动的WPF程序,这些程序是互相独立的,因此不会出现多个用户同时访问的问题。
表单传值和querystring[]传值的方法是因为ASP.NET背后的HTTP协议是无状态协议,因此需要在不同的页面之间(因为Web服务器不会记住各页面的状态)传递信息。 而WPF程序里面的各个page都是运行在同一个进程空间内,共享同一块内存,所以用Application.Current.Properties就可以了。
-
private void Jt(string param)
-
{
-
//给接谈传递页面参数
-
Application.Current.Properties["pjt"] = param;
-
SendInfo = "{ \"header\":\"测试\" , \"url\":\"/Pages/PJt.xaml\" }";
-
//消息传递给PindexModel接收,打开新的页面,第二个参数为消息key,控制接收对象
-
Messenger.Default.Send<String>(SendInfo, "AddTab");
-
-
}
-
public PJtViewModel(string dateTime)
-
{
-
string param=(string)Application.Current.Properties["pjt"];
-
Model = GetXfInfo(param);
-
GetXfjPcCount(Model);
-
-
//接收其他页面传递的消息,第二个参数为消息key,控制接收对象
-
Messenger.Default.Register<String>(this, "Xfpc_xtxfsx", ReceiveXfpcXtxfsx);
-
Messenger.Default.Register<String>(this, "Xfpc_sqyy", ReceiveXfpcSqyy);
-
Messenger.Default.Register<String>(this, "Xfpc_ccxf", ReceiveXfpcCcxf);
-
-
-
-
Messenger.Default.Register<string[]>(this, MsgTypes.列表信息控制_Callback, res =>
-
{
-
switch (res[0])
-
{
-
case "GKXX":
-
Model.Zysshtml = res[1];
-
break;
-
}
-
});
-
}