Replies: 1 comment 1 reply
-
Background property accepts IBrush, so when you give it certain string value like the built in color (e.g. Red, Blue ...) it will work fine, if you want to work with hex colors you can make a custom converter, using System.Globalization;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Media;
namespace MyProject.Converters;
public class UseHex : IValueConverter
{
public static readonly UseHex Instance = new();
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string targetColor)
{
var myColor = System.Drawing.ColorTranslator.FromHtml(targetColor);
return color_for_xaml(myColor);
}
// converter used for the wrong type
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}
public IBrush color_for_xaml(System.Drawing.Color c)
{
var clr = new Color(c.A, c.R, c.G, c.B);
return new LinearGradientBrush
{
GradientStops = new GradientStops { new GradientStop(clr, 0), new GradientStop(clr, 1) }
};
}
public object ConvertBack(
object? value,
Type targetType,
object? parameter,
CultureInfo culture
)
{
throw new NotSupportedException();
}
} <!-- make sure you import the converter -->
<UserControl xmlns:local="clr-namespace:MyProject.Converters">
<UserControl.Resources>
<local:UseHex x:Key="useHex" />
</UserControl.Resources>
<!-- -->
<Setter Property="Background" Value="{Binding BackColor,Converter={StaticResource useHex}}"/> |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When I try to bind data in Animation Setter, it is not working.
View:
ViewModel:
When I push the button, nothing happens. However, when I replace "{Biniding BackColor}" with "Red" or sime other color, it works perfectly.
Thanks in advance:
Imre
Beta Was this translation helpful? Give feedback.
All reactions