TabStrip.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Linq;
  5. using System.Reactive.Linq;
  6. using Perspex.Controls.Generators;
  7. using Perspex.Input;
  8. namespace Perspex.Controls.Primitives
  9. {
  10. public class TabStrip : SelectingItemsControl
  11. {
  12. public static readonly PerspexProperty<TabItem> SelectedTabProperty =
  13. TabControl.SelectedTabProperty.AddOwner<TabStrip>();
  14. static TabStrip()
  15. {
  16. SelectionModeProperty.OverrideDefaultValue<TabStrip>(SelectionMode.AlwaysSelected);
  17. FocusableProperty.OverrideDefaultValue(typeof(TabStrip), false);
  18. }
  19. public TabStrip()
  20. {
  21. GetObservable(SelectedItemProperty).Subscribe(x => SelectedTab = x as TabItem);
  22. GetObservable(SelectedTabProperty).Subscribe(x => SelectedItem = x as TabItem);
  23. }
  24. public TabItem SelectedTab
  25. {
  26. get { return GetValue(SelectedTabProperty); }
  27. set { SetValue(SelectedTabProperty, value); }
  28. }
  29. protected override IItemContainerGenerator CreateItemContainerGenerator()
  30. {
  31. TabControl tabControl = TemplatedParent as TabControl;
  32. IItemContainerGenerator result;
  33. if (tabControl != null)
  34. {
  35. result = tabControl.ItemContainerGenerator;
  36. }
  37. else
  38. {
  39. result = new ItemContainerGenerator<TabItem>(this, TabItem.ContentProperty);
  40. }
  41. return result;
  42. }
  43. /// <inheritdoc/>
  44. protected override void OnGotFocus(GotFocusEventArgs e)
  45. {
  46. base.OnGotFocus(e);
  47. if (e.NavigationMethod == NavigationMethod.Directional)
  48. {
  49. e.Handled = UpdateSelectionFromEventSource(e.Source);
  50. }
  51. }
  52. /// <inheritdoc/>
  53. protected override void OnPointerPressed(PointerPressEventArgs e)
  54. {
  55. base.OnPointerPressed(e);
  56. if (e.MouseButton == MouseButton.Left)
  57. {
  58. e.Handled = UpdateSelectionFromEventSource(e.Source);
  59. }
  60. }
  61. }
  62. }