-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Add a way of setting style classes using bindings #2427
Comments
Yes please, that's exactly what's missing to replace DataTriggers Pros
Cons:
Syntax: Control(MyDataContextProperty=Value) |
Is there any workarounds now? Since a ViewModel cannot access Controls to call |
You could write a behavior that is adding removing classes on some condition |
It should be possible to solve problem from another issue with this feature #4184 <Button>
<Button.Classes>
<!-- Property is read from DataContext, Value could be Binding also -->
<ClassSetter Name="PropEqualToValue" Property="{Binding Property}" Value="Value" />
</Button.Casses>
</Button>
...
<Style Selector="Button.PropEqualToValue" /> Or with boolean operators #4122 <Button>
<Button.Classes>
<ClassSetter Name="PropEqualToValue" Trigger="{Binding Property == Value}" />
</Button.Casses>
</Button> Or with functions in binding #2493 <Button>
<Button.Classes>
<ClassSetter Name="PropEqualToValue" Trigger="{Binding Property.Equals(Value)}" />
</Button.Casses>
</Button> |
Just created behavior for this. |
Implemented with New syntax is: <Button Classes.myClass="{Binding xxx}"/> |
Does the {binding property == value} get supported in current version(10.04)? Or there is no plan to implement on this? |
@Yuuon no, and there is plans to improve bindings, but there no clear design for that. This issue was about setting style classes with bindings. Personally I prefer UWP style, where you can write "{x:Bind ImageIndex.Equals(0)}", but there is no ETA for anything. So far you can use converters or create additional boolean properties in your VM per each radiobutton. |
This is nice for some scenarios, but not very convenient for others. <Border Classes.status-draft="{Binding Status, Converter={StaticResource eq}, ConverterParameter={x:Static model:OrderStatus.Draft}}"
Classes.status-abandoned="{Binding Status, Converter={StaticResource eq}, ConverterParameter={x:Static model:OrderStatus.Abandoned}}"
Classes.status-paid="{Binding Status, Converter={StaticResource eq}, ConverterParameter={x:Static model:OrderStatus.Paid}}"
Classes.status-delivered="{Binding Status, Converter={StaticResource eq}, ConverterParameter={x:Static model:OrderStatus.Delivered}}">
... This is extremely verbose, and if I had a new status, I have to add one more line like this. It would be nice to be able to do something like |
Managed to get around it with attached property.
And the usage:
|
I don't know if it's still relevant, but now, thanks to you, I can style my control based on an enum in a one-liner. <Button ext:BindableStyleClasses.Classes="{Binding Game.State, Converter={StaticResource gamePlayingStateConverter}}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Tag="{Binding Game.Id}"/> And the converter public class GamePlayingStateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
if (value is GameState playState)
{
return playState switch
{
GameState.Running => "mainLogoBar green playing",
GameState.Available => "mainLogoBar blue available",
GameState.Updating => "mainLogoBar green updating",
_ => "mainLogoBar blue available"
};
}
throw new NotSupportedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
} |
We really need a way to be able to apply to apply style classes to controls based on values from view models. This would be needed to e.g. trigger animations from the view model.
AvaloniaBehaviors has a behavior for this, but I think we should really have something in-box.
A few ideas:
Simple binding
<Button Classes="{Binding ButtonClasses}">
Pros:
Cons:
Class Setters
Access DataContext from Selectors
An alternative to binding classes would be to allow selectors to select into the DataContext.
Pros:
Cons:
The text was updated successfully, but these errors were encountered: