refactor: simpfy SourceGit.Views.BranchTreeNodeIcon

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-16 13:43:41 +08:00
parent fd935259aa
commit 1f0ab2bfec
No known key found for this signature in database
2 changed files with 13 additions and 17 deletions

View file

@ -55,9 +55,7 @@
DoubleTapped="OnDoubleTappedBranchNode"> DoubleTapped="OnDoubleTappedBranchNode">
<!-- Icon --> <!-- Icon -->
<v:BranchTreeNodeIcon Grid.Column="0" <v:BranchTreeNodeIcon Grid.Column="0" IsExpanded="{Binding IsExpanded}"/>
Node="{Binding}"
IsExpanded="{Binding IsExpanded}"/>
<!-- Name --> <!-- Name -->
<TextBlock Grid.Column="1" <TextBlock Grid.Column="1"

View file

@ -17,15 +17,6 @@ namespace SourceGit.Views
{ {
public class BranchTreeNodeIcon : UserControl public class BranchTreeNodeIcon : UserControl
{ {
public static readonly StyledProperty<ViewModels.BranchTreeNode> NodeProperty =
AvaloniaProperty.Register<BranchTreeNodeIcon, ViewModels.BranchTreeNode>(nameof(Node));
public ViewModels.BranchTreeNode Node
{
get => GetValue(NodeProperty);
set => SetValue(NodeProperty, value);
}
public static readonly StyledProperty<bool> IsExpandedProperty = public static readonly StyledProperty<bool> IsExpandedProperty =
AvaloniaProperty.Register<BranchTreeNodeIcon, bool>(nameof(IsExpanded)); AvaloniaProperty.Register<BranchTreeNodeIcon, bool>(nameof(IsExpanded));
@ -35,16 +26,23 @@ namespace SourceGit.Views
set => SetValue(IsExpandedProperty, value); set => SetValue(IsExpandedProperty, value);
} }
static BranchTreeNodeIcon() protected override void OnDataContextChanged(EventArgs e)
{ {
NodeProperty.Changed.AddClassHandler<BranchTreeNodeIcon>((icon, _) => icon.UpdateContent()); base.OnDataContextChanged(e);
IsExpandedProperty.Changed.AddClassHandler<BranchTreeNodeIcon>((icon, _) => icon.UpdateContent()); UpdateContent();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == IsExpandedProperty)
UpdateContent();
} }
private void UpdateContent() private void UpdateContent()
{ {
var node = Node; if (DataContext is not ViewModels.BranchTreeNode node)
if (node == null)
{ {
Content = null; Content = null;
return; return;