Преглед изворни кода

Make IsSelected an attached property.

Make `IsSelected` an attached property on `SelectingItemsControl` and make it bind two-way. Follows the pattern defined in WPF.
Steven Kirk пре 2 година
родитељ
комит
594346e0a5

+ 2 - 1
src/Avalonia.Controls/ListBoxItem.cs

@@ -1,6 +1,7 @@
 using Avalonia.Automation.Peers;
 using Avalonia.Controls.Metadata;
 using Avalonia.Controls.Mixins;
+using Avalonia.Controls.Primitives;
 
 namespace Avalonia.Controls
 {
@@ -14,7 +15,7 @@ namespace Avalonia.Controls
         /// Defines the <see cref="IsSelected"/> property.
         /// </summary>
         public static readonly StyledProperty<bool> IsSelectedProperty =
-            AvaloniaProperty.Register<ListBoxItem, bool>(nameof(IsSelected));
+            SelectingItemsControl.IsSelectedProperty.AddOwner<ListBoxItem>();
 
         /// <summary>
         /// Initializes static members of the <see cref="ListBoxItem"/> class.

+ 1 - 1
src/Avalonia.Controls/MenuItem.cs

@@ -57,7 +57,7 @@ namespace Avalonia.Controls
         /// Defines the <see cref="IsSelected"/> property.
         /// </summary>
         public static readonly StyledProperty<bool> IsSelectedProperty =
-            ListBoxItem.IsSelectedProperty.AddOwner<MenuItem>();
+            SelectingItemsControl.IsSelectedProperty.AddOwner<MenuItem>();
 
         /// <summary>
         /// Defines the <see cref="IsSubMenuOpen"/> property.

+ 23 - 0
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@@ -104,6 +104,14 @@ namespace Avalonia.Controls.Primitives
             AvaloniaProperty.Register<SelectingItemsControl, SelectionMode>(
                 nameof(SelectionMode));
 
+        /// <summary>
+        /// Defines the IsSelected attached property.
+        /// </summary>
+        public static readonly StyledProperty<bool> IsSelectedProperty =
+            AvaloniaProperty.RegisterAttached<SelectingItemsControl, Control, bool>(
+                "IsSelected",
+                defaultBindingMode: BindingMode.TwoWay);
+
         /// <summary>
         /// Defines the <see cref="IsTextSearchEnabled"/> property.
         /// </summary>
@@ -420,6 +428,21 @@ namespace Avalonia.Controls.Primitives
         /// <param name="item">The item.</param>
         public void ScrollIntoView(object item) => ScrollIntoView(ItemsView.IndexOf(item));
 
+        /// <summary>
+        /// Gets the value of the <see cref="IsSelectedProperty"/> on the specified control.
+        /// </summary>
+        /// <param name="control">The control.</param>
+        /// <returns>The value of the attached property.</returns>
+        public static bool GetIsSelected(Control control) => control.GetValue(IsSelectedProperty);
+
+        /// <summary>
+        /// Gets the value of the <see cref="IsSelectedProperty"/> on the specified control.
+        /// </summary>
+        /// <param name="control">The control.</param>
+        /// <param name="value">The value of the property.</param>
+        /// <returns>The value of the attached property.</returns>
+        public static void SetIsSelected(Control control, bool value) => control.SetValue(IsSelectedProperty, value);
+
         /// <summary>
         /// Tries to get the container that was the source of an event.
         /// </summary>

+ 1 - 1
src/Avalonia.Controls/TabItem.cs

@@ -22,7 +22,7 @@ namespace Avalonia.Controls
         /// Defines the <see cref="IsSelected"/> property.
         /// </summary>
         public static readonly StyledProperty<bool> IsSelectedProperty =
-            ListBoxItem.IsSelectedProperty.AddOwner<TabItem>();
+            SelectingItemsControl.IsSelectedProperty.AddOwner<TabItem>();
 
         /// <summary>
         /// Initializes static members of the <see cref="TabItem"/> class.

+ 1 - 1
src/Avalonia.Controls/TreeViewItem.cs

@@ -31,7 +31,7 @@ namespace Avalonia.Controls
         /// Defines the <see cref="IsSelected"/> property.
         /// </summary>
         public static readonly StyledProperty<bool> IsSelectedProperty =
-            ListBoxItem.IsSelectedProperty.AddOwner<TreeViewItem>();
+            SelectingItemsControl.IsSelectedProperty.AddOwner<TreeViewItem>();
 
         /// <summary>
         /// Defines the <see cref="Level"/> property.