Browse Source

Merge pull request #8829 from robloo/colorpicker-palette-binding-fix

Add DoNothingForNullConverter to Fix Color Palette Binding
pull/8920/head
Max Katz 4 years ago
committed by GitHub
parent
commit
8a8e805c58
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      src/Avalonia.Controls.ColorPicker/Converters/DoNothingForNullConverter.cs
  2. 3
      src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml

35
src/Avalonia.Controls.ColorPicker/Converters/DoNothingForNullConverter.cs

@ -0,0 +1,35 @@
using System;
using System.Globalization;
using Avalonia.Data;
using Avalonia.Data.Converters;
namespace Avalonia.Controls.Converters
{
/// <summary>
/// Converter that will do nothing (not update bound values) when a null value is encountered.
/// This converter enables binding nullable with non-nullable properties in some scenarios.
/// </summary>
public class DoNothingForNullConverter : IValueConverter
{
/// <inheritdoc/>
public object? Convert(
object? value,
Type targetType,
object? parameter,
CultureInfo culture)
{
return value ?? BindingOperations.DoNothing;
}
/// <inheritdoc/>
public object? ConvertBack(
object? value,
Type targetType,
object? parameter,
CultureInfo culture)
{
return value ?? BindingOperations.DoNothing;
}
}
}

3
src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml

@ -11,6 +11,7 @@
<pc:ThirdComponentConverter x:Key="ThirdComponentConverter" />
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
<converters:ColorToHexConverter x:Key="ColorToHexConverter" />
<converters:DoNothingForNullConverter x:Key="DoNothingForNullConverter" />
<globalization:NumberFormatInfo x:Key="ColorViewComponentNumberFormat" NumberDecimalDigits="0" />
<x:Double x:Key="ColorViewTabStripHeight">48</x:Double>
<x:Double x:Key="ColorViewComponentLabelWidth">30</x:Double>
@ -205,7 +206,7 @@
</Border>
</TabItem.Header>
<ListBox Items="{TemplateBinding PaletteColors}"
SelectedItem="{Binding Color, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
SelectedItem="{Binding Color, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource DoNothingForNullConverter}, Mode=TwoWay}"
UseLayoutRounding="False"
Margin="12">
<ListBox.Styles>

Loading…
Cancel
Save