Skip to content

Commit

Permalink
use BindingOperations.DoNothing if Data is null
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Nov 1, 2024
1 parent 1147545 commit fd9f663
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Avalonia.Data;
using System;
using System.Globalization;

namespace IconPacks.Avalonia.Converter
{
public class NullToDoNothingValueConverter : MarkupConverter
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static NullToDoNothingValueConverter()
{
}

public NullToDoNothingValueConverter()
{
}

public static NullToDoNothingValueConverter Instance { get; } = new();

/// <inheritdoc />
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Instance;
}

/// <inheritdoc />
protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value ?? BindingOperations.DoNothing;
}

/// <inheritdoc />
protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return BindingOperations.DoNothing;
}
}
}
4 changes: 2 additions & 2 deletions src/IconPacks.Avalonia.Core/PackIconControlBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
this.UpdateRotateTransformation(change.GetNewValue<double>());
}
}

// Update Spin-Animation as needed
if (change.Property == SpinProperty
|| change.Property == IsVisibleProperty
Expand All @@ -112,7 +112,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
|| change.Property == SpinEasingFunctionProperty)
{
this.StopSpinAnimation();

if (this.CanSpin())
{
this.BeginSpinAnimation();
Expand Down
2 changes: 1 addition & 1 deletion src/IconPacks.Avalonia.Core/PackIconControlBase.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Viewbox x:Name="PART_ViewBox"
Margin="{TemplateBinding Padding}">
<Path x:Name="PART_IconPath"
Data="{Binding Data, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, Converter={x:Static converter:NullToUnsetValueConverter.Instance}}"
Data="{Binding Data, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, Converter={x:Static converter:NullToDoNothingValueConverter.Instance}}"
Stretch="Uniform"
UseLayoutRounding="False" />
</Viewbox>
Expand Down

0 comments on commit fd9f663

Please sign in to comment.