mirror of
https://gitee.com/handyorg/HandyControl.git
synced 2024-11-29 18:38:30 +08:00
enhance: add ListBoxAttach.
This commit is contained in:
parent
3455fb7910
commit
456390b775
@ -0,0 +1,66 @@
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using HandyControl.Data;
|
||||
|
||||
namespace HandyControl.Controls;
|
||||
|
||||
public class ListBoxAttach
|
||||
{
|
||||
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
|
||||
"SelectedItems", typeof(IList), typeof(ListBoxAttach),
|
||||
new FrameworkPropertyMetadata(default(IList), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
||||
OnSelectedItemsChanged));
|
||||
|
||||
public static void SetSelectedItems(DependencyObject element, IList value)
|
||||
=> element.SetValue(SelectedItemsProperty, value);
|
||||
|
||||
public static IList GetSelectedItems(DependencyObject element)
|
||||
=> (IList) element.GetValue(SelectedItemsProperty);
|
||||
|
||||
internal static readonly DependencyProperty InternalActionProperty = DependencyProperty.RegisterAttached(
|
||||
"InternalAction", typeof(bool), typeof(ListBoxAttach), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
internal static void SetInternalAction(DependencyObject element, bool value)
|
||||
=> element.SetValue(InternalActionProperty, ValueBoxes.BooleanBox(value));
|
||||
|
||||
internal static bool GetInternalAction(DependencyObject element)
|
||||
=> (bool) element.GetValue(InternalActionProperty);
|
||||
|
||||
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is not ListBox listBox)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (GetInternalAction(listBox))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
listBox.SelectionChanged -= OnListBoxSelectionChanged;
|
||||
listBox.SelectedItems.Clear();
|
||||
|
||||
if (e.NewValue is IList selectedItems)
|
||||
{
|
||||
foreach (object selectedItem in selectedItems)
|
||||
{
|
||||
listBox.SelectedItems.Add(selectedItem);
|
||||
}
|
||||
}
|
||||
|
||||
listBox.SelectionChanged += OnListBoxSelectionChanged;
|
||||
}
|
||||
|
||||
private static void OnListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (sender is ListBox listBox)
|
||||
{
|
||||
SetInternalAction(listBox, true);
|
||||
SetSelectedItems(listBox, listBox.SelectedItems.Cast<object>().ToArray());
|
||||
SetInternalAction(listBox, false);
|
||||
}
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\IconSwitchElement.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\ImageAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\InfoElement.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\ListBoxAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\MenuAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\MenuTopLineAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PanelElement.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user