AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.17k stars 2.27k forks source link

Converters for resources #15688

Open styris-ame opened 6 months ago

styris-ame commented 6 months ago

Is your feature request related to a problem? Please describe.

See the following XAML that uses a binding:

<TextBlock Text="Example">
    <TextBlock.Foreground>
        <SolidColorBrush Opacity="0.5" Color="{Binding ExampleBrush, Converter={StaticResource ToColorConverter}}"/>
    </TextBlock.Foreground>
</TextBlock>

I can use the ToColorConverter to convert extract the Color property from ExampleBrush.

However, I cannot do the same with a resource:

<TextBlock Text="Example">
    <TextBlock.Foreground>
        <SolidColorBrush Opacity="0.5" Color="{DynamicResource ExampleBrush, Converter={StaticResource ToColorConverter}}"/>
    </TextBlock.Foreground>
</TextBlock>

This is particularly annoying when using themes, as I am forced to define both a SolidColorBrush and a Color for certain things.

Describe the solution you'd like

The ability to use converters with resources, or else an easy way to use a SolidColorBrush resource as a Color.

Describe alternatives you've considered

Defining a Color and a SolidColorBrush for the same actual color.

Additional context

Avalonia 11.1.0-beta2

timunie commented 6 months ago

Can you try if that would work for you? Not sure rn but worth a try. {Binding Source={DynamicResource ...}, Converter='...'}

styris-ame commented 6 months ago

Can you try if that would work for you? Not sure rn but worth a try. {Binding Source={DynamicResource ...}, Converter='...'}

It says {DynamicResource} can only be used with dependency property