AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
24.76k stars 2.15k forks source link

SelectingItemsControl missing default control styling #11139

Open thexamlguy opened 1 year ago

thexamlguy commented 1 year ago

Should work as similar to using a ItemsControl for displaying data, albeit if you want a barebone minimal control without any of the selection visual found in the ListBox control but still have the ability to control your own visual when an item is selected.

Example scenario:

        <SelectingItemsControl
            AutoScrollToSelectedItem="True"
            Grid.IsSharedSizeScope="True"
            ItemsSource="{Binding}"
            SelectedItem="{Binding Selected}">
            <SelectingItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,0,0,8">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" SharedSizeGroup="LabelColumn" />
                            <ColumnDefinition Width="25" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <HighlightTextBlock
                            Grid.Column="0"
                            HighlightText="{Binding Key.Selection}"
                            SelectionIndex="{Binding Key.SelectionIndex, Mode=TwoWay}"
                            Text="{Binding Key.Value}" />
                        <HighlightTextBlock
                            Grid.Column="2"
                            HighlightText="{Binding Value.Selection}"
                            Opacity="0.6"
                            SelectionIndex="{Binding Value.SelectionIndex, Mode=TwoWay}"
                            Text="{Binding Value.Value}" />
                    </Grid>
                </DataTemplate>
            </SelectingItemsControl.ItemTemplate>
        </SelectingItemsControl>

I've had to manually added a control style copied from ItemsControl.


    <ControlTheme x:Key="{x:Type SelectingItemsControl}" TargetType="SelectingItemsControl">
        <Setter Property="Template">
            <ControlTemplate>
                <Border
                    Padding="{TemplateBinding Padding}"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    CornerRadius="{TemplateBinding CornerRadius}">
                    <ItemsPresenter
                        Name="PART_ItemsPresenter"
                        AreHorizontalSnapPointsRegular="{TemplateBinding AreHorizontalSnapPointsRegular}"
                        AreVerticalSnapPointsRegular="{TemplateBinding AreVerticalSnapPointsRegular}"
                        ItemsPanel="{TemplateBinding ItemsPanel}" />
                </Border>
            </ControlTemplate>
        </Setter>
    </ControlTheme>
grokys commented 1 year ago

Sorry, I'm not sure what you're asking for. Could you try to reword it?

thexamlguy commented 1 year ago

Sorry, I'm not sure what you're asking for. Could you try to reword it?

Sorry!

At the moment, if one was to consume a SelectingItemsControl directly, nothing is displayed. Maybe we could go about adding a default ControlTheme for SelectingItemsControl for both Fluent and Simple theme? Developers can then consume the SelectingItemsControl without requiring to manually add the SelectingItemsControl ControlTheme theme resources themselves, similar to how ItemsControl work out of the box.

Happy to contribute to this one if accepted.

robloo commented 1 year ago

ItemsControl is usable on its own. I think SelectingItemsControl should be as well. For me it makes sense to add default control themes for this.

grokys commented 1 year ago

There are two problems with this:

I fear by making this control have a visual representation but one which doesn't actually indicate selection, we'd be confusing things even more. It would probably make more sense to make the control abstract IMO.

wieslawsoltes commented 1 year ago

There are two problems with this:

  • SelectingItemsControl doesn't specify an item container type, so how would selected items be marked as selected? For example if one's ItemsSource contains a list of strings then the created containers would be ContentPresenters. How would a ContentPresenter visually indicate that it's selected?
  • SelectingItemsControl provides no user interaction handling, so even if you got selection displaying, the user wouldn't be able to interact with the control.

I fear by making this control have a visual representation but one which doesn't actually indicate selection, we'd be confusing things even more. It would probably make more sense to make the control abstract IMO.

I use a lot the SelectingItemsControl and each time I have to do custom template based on ItemsControl. It is really useful control please don't make it abstract. It serves great use case between ItemsControl and ListBox

grokys commented 1 year ago

Interesting. How do you deal with the SelectingItemsControl not having a container type @wieslawsoltes ?

wieslawsoltes commented 1 year ago

Interesting. How do you deal with the SelectingItemsControl not having a container type @wieslawsoltes ?

This is one if my usages:

https://github.com/wieslawsoltes/ChatGPT/blob/153a760b6cbf240bdd2930ca2154d6e261c096a5/src/ChatGPT.UI/Views/Chat/ChatView.axaml#L28

https://github.com/wieslawsoltes/ChatGPT/blob/153a760b6cbf240bdd2930ca2154d6e261c096a5/src/ChatGPT.UI/Themes/SelectingItemsControl.axaml#L3