AvaloniaUI/Avalonia.Markup.Declarative

Style Support - Enhancement Request

najak3d opened this issue · 2 comments

I might be missing something here, but it seems to me that Markup.Declarative has weak support for styling.

I'd like to see something more built-in, such as:

  1. Enabling a Style Override in C#. (something that says "for this scope apply these style overrides, per control type").
    It would behave similar to AXAML statements like these (from Dock demo):
        <Grid.Styles>
          <Style Selector="Button">
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
          </Style>
          <Style Selector="TextBox">
            <Setter Property="MinHeight" Value="0" />
            <Setter Property="Margin" Value="4,0,4,0" />
            <Setter Property="Padding" Value="4" />
          </Style>
        </Grid.Styles>
  1. Fully C#-implemented Styles. So that we could inherit from styles and override, or add new members/etc.
    This would need to fully replace the AXAML used now for defining Fluent/Default styles, etc.

I'm adding such way to define styles:

  //Typed style definition
  new Style<Button>(s => s.Class("selected"))
      .Background(StaticResources.Brushes.AccentBrush),

  //General style definition
  new Style(s => s.OfType<Button>().Class("selected"))
      .Setter(TemplatedControl.BackgroundProperty, StaticResources.Brushes.AccentBrush)

i cant use styles. this is the xaml. the xaml below is defined inside a control theme that targets buttons:

<Setter Property="Width" Value="{Binding NormalPaneWidth, ElementName=thisHamburgerView}" />
<Setter Property="Height" Value="{Binding NormalPaneWidth, ElementName=thisHamburgerView}" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Black" />
<Style Selector="^:pointerover">
	<Setter Property="Background" Value="Red" />
</Style>

c#:

new Button()
	.HorizontalAlignment(Avalonia.Layout.HorizontalAlignment.Left)
	.Width(NormalPaneWidth)
	.Height(NormalPaneWidth)
	.Padding(0)
	.BorderThickness(0)
	.Background(Brushes.Black)
	.Styles(
		new Style<Button>(s => s.Class("^:pointerover"))
			.Background(Brushes.Red))