nth-child selector
See original GitHub issueIs your feature request related to a problem? Please describe.
We had some talk about this internally, but I am repeating it here for visibility: It would be nice to have some CSS-like nth-child
selector that allows you to style every nth descendant.
Describe the solution you’d like
<Style Selector="ListBox ListBoxItem:nth-child(2n+2)" />
(Would select every 2nd ListBoxItem)
The syntax is debatable, but it’s how CSS does it: https://css-tricks.com/how-nth-child-works/
Describe alternatives you’ve considered
WPF’s ItemsControl.AlternationIndex is not flexible enough IMO.
Additional context
CC: @grokys
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:14 (13 by maintainers)
Top Results From Across the Web
nth-child() - CSS: Cascading Style Sheets - MDN Web Docs
:nth-child() takes a single argument that describes a pattern for matching element indices in a list of siblings. Element indices are 1-based.
Read more >CSS :nth-child() Selector
The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or...
Read more >nth-child
The :nth-child selector allows you to select one or more elements based on their source order, according to a formula.
Read more >Matching elements in CSS with :nth-child and other selectors
The :nth-child selector in CSS allows us to select and style a specific element based on its position in the parent container. Suppose...
Read more >:nth-child() Selector | jQuery API Documentation
Description: Selects all elements that are the nth-child of their parent. version added: 1.1.4jQuery( ":nth-child(index/even/odd/equation)" ).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Based on @maxkatz6 code I have created reusable behavior:
https://github.com/wieslawsoltes/AvaloniaBehaviors/blob/4aa7ce1334b6208ca7321c06783ea5f47ca07d48/src/Avalonia.Xaml.Interactions/Custom/NthChildBehavior.cs#L18
Sample usage:
https://github.com/wieslawsoltes/AvaloniaBehaviors/blob/4aa7ce1334b6208ca7321c06783ea5f47ca07d48/samples/BehaviorsTestApplication/Views/Pages/NthChildBehaviorView.axaml#L17
Good you mentioned it here. I did created simplifed classes setter that can be enabled with attached properties. Will be useful for anybody, who needs it, but can’t wait until Avalonia will get proper support for it. https://gist.github.com/maxkatz6/124dc79665462982c674b19d788ab6de