feat: mark container from external selection change. clean up code

This commit is contained in:
rabbitism
2024-04-17 01:11:25 +08:00
parent 887242d4a6
commit 13a9806e4b

View File

@@ -93,15 +93,8 @@ public class TreeComboBox: ItemsControl
public object? SelectedItem public object? SelectedItem
{ {
get get => _selectedItem;
{ set => SetAndRaise(SelectedItemProperty, ref _selectedItem, value);
return _selectedItem;
}
set
{
SetAndRaise(SelectedItemProperty, ref _selectedItem, value);
}
} }
static TreeComboBox() static TreeComboBox()
@@ -113,10 +106,9 @@ public class TreeComboBox: ItemsControl
private void OnSelectedItemChanged(AvaloniaPropertyChangedEventArgs<object?> args) private void OnSelectedItemChanged(AvaloniaPropertyChangedEventArgs<object?> args)
{ {
if (args.NewValue.Value is not null) MarkContainerSelection(args.OldValue.Value, false);
{ MarkContainerSelection(args.NewValue.Value, true);
UpdateSelectionBoxItem(args.NewValue.Value); UpdateSelectionBoxItem(args.NewValue.Value);
}
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@@ -183,6 +175,7 @@ public class TreeComboBox: ItemsControl
private void UpdateSelectionBoxItem(object? item) private void UpdateSelectionBoxItem(object? item)
{ {
if(item is null) SelectionBoxItem = null;
if (item is ContentControl contentControl) if (item is ContentControl contentControl)
{ {
item = contentControl.Content; item = contentControl.Content;
@@ -280,4 +273,14 @@ public class TreeComboBox: ItemsControl
} }
return null; return null;
} }
private void MarkContainerSelection(object? item, bool selected)
{
if (item is null) return;
var container = TreeContainerFromItem(item);
if (container is TreeComboBoxItem treeComboBoxItem)
{
treeComboBoxItem.IsSelected = selected;
}
}
} }