Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
Greatly improved TabbedLayout behavior when bound to Categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Vuyka committed Mar 10, 2011
1 parent 6080ef6 commit 824c93f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
55 changes: 55 additions & 0 deletions Main/WpfPropertyGrid/Design/TabbedLayout.cs
Expand Up @@ -25,6 +25,8 @@ namespace System.Windows.Controls.WpfPropertyGrid.Design
/// </summary>
public class TabbedLayout : TabControl
{
private static readonly IValueConverter visibilityConverter = new BooleanToVisibilityConverter();

/// <summary>
/// The fallback header for a tab if no header custom is provided.
/// </summary>
Expand Down Expand Up @@ -229,9 +231,62 @@ protected override void PrepareContainerForItemOverride(DependencyObject element

tab.SetBinding(HeaderedContentControl.HeaderProperty, bHeader);
}

if (item is GridEntry)
{
var binding = new Binding("IsVisible")
{
Source = item,
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = visibilityConverter
};
tab.SetBinding(UIElement.VisibilityProperty, binding);
}

tab.IsVisibleChanged += OnTabVisibilityChanged;
}
}

private void OnTabVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var tabItem = sender as TabbedLayoutItem;
if (tabItem == null || tabItem.DataContext == null) return;

bool isVisible = (bool)e.NewValue;


if (isVisible && GetVisibleEntryCount() == 1)
{
var visibleEntry = GetFirstVisibleEntry();
if (SelectedItem != visibleEntry)
SelectedItem = visibleEntry;
}
else if (tabItem.IsSelected)
{
if (GetVisibleEntryCount() == 0)
{
SelectedItem = null;
return;
}

if (Items.IndexOf(tabItem.DataContext) > 0)
SelectedIndex--;
else if (Items.Count > 1)
SelectedIndex++;
}
}

private GridEntry GetFirstVisibleEntry()
{
return Items.OfType<GridEntry>().FirstOrDefault(item => item.IsVisible);
}

private int GetVisibleEntryCount()
{
return Items.OfType<GridEntry>().Count(item => item.IsVisible);
}

/// <summary>
/// Determines if the specified item is (or is eligible to be) its own ItemContainer.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion Main/WpfPropertyGrid/Design/TabbedLayout.xaml
Expand Up @@ -7,7 +7,12 @@

<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>

<Style TargetType="{x:Type design:TabbedLayout}" BasedOn="{StaticResource {x:Type TabControl}}"/>
<Style TargetType="{x:Type design:TabbedLayout}" BasedOn="{StaticResource {x:Type TabControl}}">
<Style.Resources>
<design:TabbedLayoutTemplateSelector x:Key="TabbedLayoutTemplateSelector"/>
</Style.Resources>
<Setter Property="ContentTemplateSelector" Value="{StaticResource TabbedLayoutTemplateSelector}"/>
</Style>

<Style x:Key="TabItemFocusVisual">
<Setter Property="Control.Template">
Expand Down
33 changes: 33 additions & 0 deletions Main/WpfPropertyGrid/Design/TabbedLayoutTemplateSelector.cs
@@ -0,0 +1,33 @@
namespace System.Windows.Controls.WpfPropertyGrid.Design
{
public class TabbedLayoutTemplateSelector : DataTemplateSelector
{
private readonly ResourceLocator _resourceLocator = new ResourceLocator();

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var category = item as CategoryItem;
if (category != null)
{
var template = FindEditorTemplate(category);
if (template != null) return template;
}

return base.SelectTemplate(item, container);
}

protected virtual DataTemplate FindEditorTemplate(CategoryItem category)
{
if (category == null) return null;

var editor = category.Editor;

if (editor == null) return null;

var template = editor.InlineTemplate as DataTemplate;
if (template != null) return template;

return _resourceLocator.GetResource(editor.InlineTemplate) as DataTemplate;
}
}
}
1 change: 1 addition & 0 deletions Main/WpfPropertyGrid/WpfPropertyGrid.csproj
Expand Up @@ -159,6 +159,7 @@
<Compile Include="DataResource.cs" />
<Compile Include="Design\GridEntryContainer.cs" />
<Compile Include="Design\GridEntryLayout.cs" />
<Compile Include="Design\TabbedLayoutTemplateSelector.cs" />
<Compile Include="EditorCollection.cs" />
<Compile Include="Controls\SearchTextBox.cs" />
<Compile Include="Design\CategoryContainer.cs" />
Expand Down

0 comments on commit 824c93f

Please sign in to comment.