question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When styling multiple sliders with class selectors, only the last selector works

See original GitHub issue

Describe the bug When there are multiple styles for sliders defined using class selectors, only the last selector is valid and works.

To Reproduce

  1. Add 2 sliders with different classes to a control.
  2. Create two styles for each slider.class (they can have the same Minimum and Maximum values).
  3. Only the slider with class that is defined last in the selectors will work.

OR use the following code:

<Window.Styles>
    <Style Selector="Slider.percentSlider">
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Maximum" Value="100"/>
    </Style>
    <Style Selector="Slider.colorSlider">
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Maximum" Value="100"/>
    </Style>

</Window.Styles>

<Design.DataContext>
    <vm:MainWindowViewModel/>
</Design.DataContext>

<StackPanel>
    <Slider Classes="colorSlider"/>
    <Slider Classes="percentSlider"/>
</StackPanel>

Expected behavior Both sliders should work correctly.

Screenshots image The second slider cannot be moved

Desktop (please complete the following information):

  • OS: Windows 10
  • Version 10.14

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
maxkatz6commented, Oct 14, 2022

@rabbitism correct, expected behavior would be to throw an exception. And it is done with that PR https://github.com/AvaloniaUI/Avalonia/pull/8600

0reactions
T-0pelcommented, Oct 14, 2022

Thanks for the detailed explanation and discussion. I agree that throwing an exception would be best since this is not supported. The use case was indeed to apply the same properties to a large number of controls, so I will try using data templates to achieve that.

One more weird thing that I do not understand - the styling of data properties seems to work correctly when I set the Maximum to a Binding. That is how it originally was in the code I have inherited. Do you know why that works if it is not supported?

image

<Window.Styles>
    <Style Selector="Slider.percentSlider">
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Maximum" Value="{Binding Percent}"/>
    </Style>
    <Style Selector="Slider.colorSlider">
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Maximum" Value="{Binding Percent2}"/>
    </Style>
</Window.Styles>

<Design.DataContext>
    <vm:MainWindowViewModel/>
</Design.DataContext>

<StackPanel>
    <TextBox Text="{Binding Slider1}"></TextBox>
    <Slider Classes="colorSlider" Value="{Binding Slider1}"/>
    <TextBox Text="{Binding Slider2}"></TextBox>
    <Slider Classes="percentSlider" Value="{Binding Slider2}"/>
</StackPanel>
public class MainWindowViewModel : ViewModelBase
{
    private int _slider1;
    private int _slider2;

    public int Percent => 100;
    public int Percent2 => 1000;

    public int Slider1
    {
        get => _slider1;
        set => this.RaiseAndSetIfChanged(ref _slider1, value);
    }

    public int Slider2
    {
        get => _slider2;
        set => this.RaiseAndSetIfChanged(ref _slider2, value);
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

last-of-type - CSS: Cascading Style Sheets - MDN Web Docs
The :last-of-type CSS pseudo-class represents the last element of its type among a group of sibling ... Multiple selectors elements.
Read more >
select second active slide in slickslider through css-selector
I am using slick slider and showing 3 slides, I would like to add specific styling on the center slide. by using nth-of-type...
Read more >
nth-last-child() - CSS: Cascading Style Sheets - MDN Web Docs
Represents every <p> element among a group of siblings. This is the same as a simple p selector. (Since n starts at zero,...
Read more >
How to select "last child" with a specific class using CSS
Approach: The CSS :last-child selector is used to target the last child element of its parent for styling. Syntax: :last-child { // CSS...
Read more >
Useful :nth-child Recipes
Select Only the Fifth Element · Select All But The First Five · Select Only The First Five · Select Every Fourth, Starting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found