Skip to content

Commit

Permalink
#212: Why not go ahead and make a proper tabbed browser while we're a…
Browse files Browse the repository at this point in the history
…t it... :)
  • Loading branch information
perlun committed Mar 21, 2014
1 parent 7ad4fb5 commit fd20a06
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 6 additions & 2 deletions CefSharp.Wpf.Example/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
Title="CefSharp.Wpf.Example"
WindowState="Maximized">

<TabControl Margin="0,5,0,0">
<!-- TODO: Add support for opening/closing tabs freely, as the user wishes -->
<TabControl x:Name="TabControl"
Margin="0,5,0,0"
SelectionChanged="OnTabControlSelectionChanged">
<!-- TODO: Change the TabItems to use a proper ItemsSource-based approach instead. This is just a big hack for now. -->
<TabItem Header="{Binding Tab1Content.DataContext.Title}"
Width="150"
Height="20">
Expand All @@ -19,5 +21,7 @@
Height="20">
<ContentControl Content="{Binding Tab2Content}" />
</TabItem>

<TabItem Header="+"/>
</TabControl>
</Window>
42 changes: 40 additions & 2 deletions CefSharp.Wpf.Example/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using CefSharp.Wpf.Example.Views.Main;
using System.Windows.Data;
using System.Windows.Input;
using CefSharp.Wpf.Example.Views.Main;
using System.Windows;
using System.Windows.Controls;

namespace CefSharp.Wpf.Example
{
Expand All @@ -18,7 +21,42 @@ public MainWindow()
DataContext = new MainViewModel { ShowSidebar = true }
};

Tab2Content = new MainView
Tab2Content = CreateNewTab();
}

private void OnTabControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count != 1) return;

var selectedtab = (TabItem)e.AddedItems[0];
if ((string)selectedtab.Header != "+")
{
return;
}

var tabItem = CreateTabItem();
TabControl.Items.Insert(TabControl.Items.Count - 1, tabItem);
}

private static TabItem CreateTabItem()
{
var tabItem = new TabItem
{
Width = 150,
Height = 20,
IsSelected = true,
Content = CreateNewTab()
};
tabItem.SetBinding(HeaderedContentControl.HeaderProperty, new Binding("Content.DataContext.Title")
{
RelativeSource = RelativeSource.Self
});
return tabItem;
}

private static MainView CreateNewTab()
{
return new MainView
{
DataContext = new MainViewModel("http://www.google.com")
};
Expand Down

0 comments on commit fd20a06

Please sign in to comment.