I
I
iRumba2015-04-22 13:04:13
XAML
iRumba, 2015-04-22 13:04:13

How to create a stylesheet in xaml?

Hello. I can't find info on google because I don't know what to look for.
How to make an analogue of html + css in xaml in terms of generalizations?
Well, for example, in css, I create a class, prescribe properties in it. I can set separate properties for each element in the class. For example, I take a div, put text, a text field and a button in it. I create a class in the style file, add a style to it for the text, for the text field and for the button. In the div I specify a class equal to the class in the style sheet and everything inside the div gets the properties specified in the style sheet.
But in Hamley, such a construction does not work. Here are the styles.

<Window.Resources>
        <Style x:Key="OneLine">
            <Setter Property="TextBox.MaxLines" Value="1"/>
            <Setter Property="TextBox.HorizontalAlignment" Value="Stretch"/>
            <Setter Property="TextBox.Width" Value="auto"/>
            <Setter Property="TextBox.Margin" Value="0,0,0,10"/>

            <Setter Property="TextBlock.Width" Value="auto"/>
            <Setter Property="TextBlock.HorizontalAlignment" Value="Stretch"/>
            <Setter Property="TextBlock.TextWrapping" Value="WrapWithOverflow"/>
            <Setter Property="TextBlock.Margin" Value="0,0,0,0"/>
            
            <Setter Property="Grid.Height" Value="auto"/>
            <Setter Property="Grid.Width" Value="auto"/>
            <Setter Property="Grid.HorizontalAlignment" Value="Stretch"/>
            <Setter Property="Grid.VerticalAlignment" Value="Top"/>
            
            <Setter Property="Label.Width" Value="auto"/>
            <Setter Property="Label.Margin" Value="10,10,10,10"/>
        </Style>           
    </Window.Resources>

I tried to use these styles in different ways in the stack panel I needed.
Tried like this:
<StackPanel HorizontalAlignment="Stretch" Height="auto" VerticalAlignment="Stretch" Width="auto" Style="{StaticResource OneLine}">

In this case, it applies to the stack panel itself, even though it is not a label. The same property applies to the panel and from the TextBlock. But they do not apply to the labels themselves inside the stack panel.
Tried like this:
<Style BasedOn="{StaticResource OneLine}">
                    
                </Style>

In this case, nothing happens.
And tried like this:
<Label Content="Прокси сервер:" Name="Label1" Style="{StaticResource OneLine}"/>

So everything works well, but I'm not a parrot, I don't want to write the same thing for each element. I want to write to their parent and force them to inherit. And at the same time, so that the parent does not take over the properties of an element that it is not (as in the first example).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir S, 2015-04-22
@iRumba

create styles separately for each element, if there are any common properties - make a separate style and then override it.

<Style BasedOn="{StaticResource {x:Type CheckBox}}" TargetType="{x:Type CheckBox}">
        <Setter Property="CheckBox.HorizontalContentAlignment" Value="Left" />
        <Setter Property="CheckBox.VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="1" />
    </Style>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question