Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Greatly improved TabbedLayout behavior when bound to Categories

  • Loading branch information...
commit 824c93f1fc283f96c2768621dcc4b840566c68c2 1 parent 6080ef6
@DenisVuyka authored
View
55 Main/WpfPropertyGrid/Design/TabbedLayout.cs
@@ -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>
@@ -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>
View
7 Main/WpfPropertyGrid/Design/TabbedLayout.xaml
@@ -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">
View
33 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;
+ }
+ }
+}
View
1  Main/WpfPropertyGrid/WpfPropertyGrid.csproj
@@ -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" />
Please sign in to comment.
Something went wrong with that request. Please try again.