VincentH-Net / CSharpForMarkup

Concise, declarative C# UI markup for .NET browser / native UI frameworks
MIT License
748 stars 43 forks source link

Default Styles are not applied to controls (tested only on Label) #31

Closed scottcg closed 2 years ago

scottcg commented 2 years ago

Default styles, such as:

<!--  This is the default style for all labels  -->
<Style TargetType="{x:Type Label}">
    <Setter Property="Foreground" Value="{DynamicResource FontColor}" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="Padding" Value="{DynamicResource LabelPadding}" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="FontSize" Value="13" />
</Style>

Are not applied to Label() in markup.

In the c# code one can lookup the style and see that it is present using this:

var defaultStyle = App.Current.TryFindResource(typeof(System.Windows.Controls.Label)) as System.Windows.Style;

When manually applying this to the Label, it still doesn't work. That may mean there's something strange with how Label deals with Content during construction. Not sure.

You can see this in the Welcome_UI.cs code file.

VincentH-Net commented 2 years ago

When I create an implicit style in Styles.cs and include that in a Dictionary that I assign to the application resources, the style does work, e,g,: Styles.cs:

public static Style<Label> Labels => labels ??= new (
    (LABEL_UI.FontSizeProperty, 30.0)
);

App.xaml.cs:

window.Resources = Styles.Implicit.Dictionary;      

However when I have this in App.xaml:

    <Application.Resources>
        <Style TargetType="{x:Type Label}">
            <Setter Property="FontSize" Value="50" />
        </Style>
    </Application.Resources>

and do not assign anything to window.Resources in App.xaml.cs, then the implicit style does not work.

Maybe I need to do something in C# to apply implicit styles defined in XAML?

VincentH-Net commented 2 years ago

Ok, solved it - not a C# Markup issue: e.g. if you have Dictionary1.xaml with:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Label}">
        <Setter Property="FontSize" Value="50" />
    </Style>
</ResourceDictionary>

and in App.xaml.cs you do:

    public App()
    {
        this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Dictionary1.xaml", UriKind.Relative) });
    }

Then it works. Apparently there is a difference between Application.Resources in App.xaml and a ResourceDictionary in a separate xaml file.

So implicit styles work both from C# and from XAML.