JohnSon:动态创建模块选项卡
[ImportingConstructor]
public VMain(VMMain main, IEventAggregator aggregator)
{
InitializeComponent();
this.imCancel.MouseLeftButtonUp += new MouseButtonEventHandler(imgCancel_MouseLeftButtonUp); this.DataContext = main;
vmMain = main;
this.eventAggregator = aggregator;
this.button1.Click += new RoutedEventHandler(ClickEvent);
this.imCancel.MouseLeftButtonUp += new MouseButtonEventHandler(imgCancel_MouseLeftButtonUp);
vmMain.ShowViewCommand.Execute("Home");
eventAggregator.GetEvent<ModuleView>().Subscribe(ShowMainView, ThreadOption.UIThread, true);
}
private void imgCancel_MouseLeftButtonUp(object sender, EventArgs e)
{
vmMain.ShowViewCommand.Execute("Login");
}
private void ClickEvent(object sender, EventArgs e)
{
Button bt =sender as Button;
string tabName = bt.Tag.ToString();
TabItem tbFind = this.tabControl.FindName(tabName) as TabItem;
if (tbFind == null)
{
vmMain.ShowViewCommand.Execute(tabName);
}
else
{
this.tabControl.SelectedItem = tbFind;
}
}
private void ShowMainView(IView moduleView)
{
string mName = moduleView.ModuleName;
ucPanel = moduleView as UserControl;
ucPanel.Name = mName + "View";
ScrollViewer scrv = new ScrollViewer();
if (mName.Equals("Home"))
{
this.tabControl.Items.Add(new TabItem { Name = "Welcom", Header = "起始页", Content = SetScrollView(scrv, ucPanel) });
return;
}
TabItem tb = new TabItem { Content = SetScrollView(scrv, ucPanel), Name = mName };
DataTemplate dt = (DataTemplate)System.Windows.Markup.XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
<Border CornerRadius=""8,8,0,0"">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""*""/>
<ColumnDefinition Width=""20""/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=""0"" Text=""{Binding}"" Margin=""0,0,5,0""></TextBlock>
<Grid Grid.Column=""1"" Cursor=""Hand"" HorizontalAlignment=""Right"" Name=""Close"" Width=""15"" Height=""15"" >
</Grid>
</Grid>
</Border>
</DataTemplate>");
var headPanel = dt.LoadContent() as Border;
headPanel.DataContext = mName;
if (headPanel != null)
{
Grid img = headPanel.FindName("Close") as Grid;
if (img != null)
{
//添加关闭事件
img.MouseLeftButtonUp += (s, ex) => { ex.Handled = true; TabItemClose(scrv, tb, img, ucPanel); };
tb.MouseEnter += (s, ex) => { ImgCloseDis(img); };
tb.MouseLeave += (s, ex) => { ImgCloseCancel(img); };
}
}
tb.Header = headPanel;
this.tabControl.Items.Add(tb);
this.tabControl.SelectedItem = tb;
}
private void TabItemClose(ScrollViewer scrv, TabItem tbi, Grid img, UserControl uc)
{
tbi.Content = null;
scrv.Content = null;
tabControl.Items.Remove(tbi);
}
private void ImgCloseDis(Grid tbi)
{
ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri(@"./Images/Close.png", UriKind.Relative));
tbi.Background = ib;
}
private void ImgCloseCancel(Grid tbi)
{
tbi.Background = null;
}
private ScrollViewer SetScrollView(ScrollViewer scrv, UserControl usercon)
{
scrv.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scrv.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
scrv.Content = usercon;
return scrv;
}