Skip to content

Commit fd20a06

Browse files
committed
#212: Why not go ahead and make a proper tabbed browser while we're at it... :)
1 parent 7ad4fb5 commit fd20a06

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

CefSharp.Wpf.Example/MainWindow.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
Title="CefSharp.Wpf.Example"
77
WindowState="Maximized">
88

9-
<TabControl Margin="0,5,0,0">
10-
<!-- TODO: Add support for opening/closing tabs freely, as the user wishes -->
9+
<TabControl x:Name="TabControl"
10+
Margin="0,5,0,0"
11+
SelectionChanged="OnTabControlSelectionChanged">
12+
<!-- TODO: Change the TabItems to use a proper ItemsSource-based approach instead. This is just a big hack for now. -->
1113
<TabItem Header="{Binding Tab1Content.DataContext.Title}"
1214
Width="150"
1315
Height="20">
@@ -19,5 +21,7 @@
1921
Height="20">
2022
<ContentControl Content="{Binding Tab2Content}" />
2123
</TabItem>
24+
25+
<TabItem Header="+"/>
2226
</TabControl>
2327
</Window>

CefSharp.Wpf.Example/MainWindow.xaml.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using CefSharp.Wpf.Example.Views.Main;
1+
using System.Windows.Data;
2+
using System.Windows.Input;
3+
using CefSharp.Wpf.Example.Views.Main;
24
using System.Windows;
5+
using System.Windows.Controls;
36

47
namespace CefSharp.Wpf.Example
58
{
@@ -18,7 +21,42 @@ public MainWindow()
1821
DataContext = new MainViewModel { ShowSidebar = true }
1922
};
2023

21-
Tab2Content = new MainView
24+
Tab2Content = CreateNewTab();
25+
}
26+
27+
private void OnTabControlSelectionChanged(object sender, SelectionChangedEventArgs e)
28+
{
29+
if (e.AddedItems.Count != 1) return;
30+
31+
var selectedtab = (TabItem)e.AddedItems[0];
32+
if ((string)selectedtab.Header != "+")
33+
{
34+
return;
35+
}
36+
37+
var tabItem = CreateTabItem();
38+
TabControl.Items.Insert(TabControl.Items.Count - 1, tabItem);
39+
}
40+
41+
private static TabItem CreateTabItem()
42+
{
43+
var tabItem = new TabItem
44+
{
45+
Width = 150,
46+
Height = 20,
47+
IsSelected = true,
48+
Content = CreateNewTab()
49+
};
50+
tabItem.SetBinding(HeaderedContentControl.HeaderProperty, new Binding("Content.DataContext.Title")
51+
{
52+
RelativeSource = RelativeSource.Self
53+
});
54+
return tabItem;
55+
}
56+
57+
private static MainView CreateNewTab()
58+
{
59+
return new MainView
2260
{
2361
DataContext = new MainViewModel("http://www.google.com")
2462
};

0 commit comments

Comments
 (0)