-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend Classes.bind
syntax to support enum and string bindings
#18068
Comments
Alternative syntax: allow binding Also, regardless of the syntax, allow returning a space delimited string to apply several classes at once. Said another way, any string valid in (Optionally, for performance, we might also accept a property or converter returning a |
Well, something similar can be implemented now. public static class BindableStyle {
public static readonly AttachedProperty<string> ClassesProperty =
AvaloniaProperty.RegisterAttached<StyledElement, string>("Classes", typeof(BindableStyle), string.Empty);
public static void SetClasses(AvaloniaObject element, string value) =>
element.SetValue(ClassesProperty, value);
public static string GetClasses(AvaloniaObject element) =>
element.GetValue(ClassesProperty);
static BindableStyle() =>
ClassesProperty.Changed.AddClassHandler<StyledElement>(OnClassesAttachedPropertyChanged);
private static void OnClassesAttachedPropertyChanged(AvaloniaObject o, AvaloniaPropertyChangedEventArgs e) {
if (o is StyledElement styled) {
styled.Classes.Replace((e.GetNewValue<string>() ?? string.Empty).Split(' '));
}
}
} <Rectangle>
<ui:BindableStyle.Classes>
<MultiBinding Converter="{StaticResource Converters.StatusItemToClasses}">
<Binding Path="Value" />
<Binding Path="Warning" />
</MultiBinding>
</ui:BindableStyle.Classes>
</Rectangle> But I agree, it would have been better without the additional code. ADD: |
Yea, we should allow a way to set one or more string value to However, I don't have a good idea in mind for this either but I would go a different route as @MrJul said. It makes the most sense to handle all the calculation and conversion of properties to class "string name" in the view model (no need to StringFormat). Then just support binding that view model string name to In fact, the real issue is perhaps we should have made Edit: Ok, ugly idea, but what if we had something like |
Is your feature request related to a problem? Please describe.
Currenly
Classes.className
syntax only supports boolean bindings, which toggle specified class name on the control:By itself this syntax is very convenient, but also very limiting for enum and string bindings.
For example, if developer wants to toggle classes depending on enum value without any C# code, they should somehow convert it to boolean expression, which can be written this:
With custom converters this syntax can be slightly improved, but far from ideal.
Describe the solution you'd like
Extend existing syntax by allowing non-boolean bindings. Combined with
StringFormat
when desired to customize property name.Where:
It should also work with setters:
Problems:
Describe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: