Skip to content
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

WPF-style styles #2765

Closed
kekekeks opened this issue Jul 23, 2019 · 4 comments
Closed

WPF-style styles #2765

kekekeks opened this issue Jul 23, 2019 · 4 comments
Assignees

Comments

@kekekeks
Copy link
Member

kekekeks commented Jul 23, 2019

Basically we need a way for controls to have their own styles that would provide the control theme.

<Button>
    <Button.Style>
         <Style> <!-- no Selector here -->
         </Style>
    </Button.Style>
</Button>
<ResourceDictionary>
   <ResourceDictionary.Resources>
        <!-- 
              Same logic as in WPF, TargetType is *only* allowed
              in resources context (enforced by compiler).
              We can have `BasedOn` as well
         -->
       <Style TargetType="{x:Button}" x:Key="{x:Button}"> 
       </Style>
   </ResourceDictionary.Resources>
</ResourceDictionary>

If there is no Style manually specified and no Style found in Resources, the system should use the default style for such control.

"Own" styles should work alongside with our current resource system, it should be possible to set the own style using a css style, e. g.

<Style Selector="Button.themed">
     <Setter Property="Style" Value="{StaticResource MyThemeButtonStyle}"/>
</Style>

This feature will

  • allow us to have proper default styles for controls without the need to add a StyleInclude for every 3rd party library
  • simplify porting WPF code
@kekekeks
Copy link
Member Author

Assigning the default style to a control

On the low level it would

interface IHaveDefaultStyle
{
       Style GetDefaultStyle();
}

implemented by the control.

From user point of view it would be a XAML file looking like this in the same assembly as the control

<Style x:DefaultStyleFor="{x:Type MyControl}">
</Style>

It would be wired into MyControl via IHaveDefaultStyle implementation generated by the XAML compiler

@kekekeks
Copy link
Member Author

Since our xaml loader depends on Avalonia.Controls we have some kind of a chicken and egg problem. To resolve that, we'll have to move all our default styles to Avalonia.Markup.Xaml assembly and write them like this:

<Style x:Class="Avalonia.Markup.Xaml.DefaultStyles.DefaultButtonStyle" TargetType="x:Type Button">
internal class DefaultButtonStyle : Style
{
}

it should allow us to reference such styles from C# code like this:

new Dictionary<Type, Style>
{
     [typeof(Button)]= new DefaultButtonStyle();
}

which could be used to resolve styles for controls from Avalonia.Controls.

@grokys
Copy link
Member

grokys commented Jul 23, 2019

I spoke about a way to specify default themes here: #1883

How does this compare?

@kekekeks
Copy link
Member Author

Superseded by #2769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants