0

I have a ListBox with an ItemsSource that can contain null values. I can't select those null values in the ListBox with the mouse, but I can with the keyboard. Is there any way to make null items selectable by mouse?

Example xaml:

<ListBox>
    <ListBox.Items>
        <x:Null />
        <system:String>Hello</system:String>
        <x:Null />
    </ListBox.Items>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Text">
                            <Setter.Value>
                                <Binding />
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=.}" Value="{x:Null}">
                                <Setter Property="Text" Value="Null value!" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Which results in:
image of example
however, I can't select any of the Null value! entries using the mouse, which is what I want to do.

If it can be done with another pure-xaml solution, that'd be fine. I'd prefer to not have to use any converters, if possible.

ManIkWeet
  • 1,298
  • 1
  • 15
  • 36
  • 2
    Null ist not an object. While you can add Null to a collection Null cannot be rendered. You should explicitly insert placeholders (or whatever Null does represent in your logic). An empty string seems to be more reasonable in your context. – BionicCode Jan 17 '22 at 17:13
  • Thing is, I can render the `null` with the style's triggers. This is a simplified example of the issue I run into, and my actual datatype can't really have a placeholder very easily. – ManIkWeet Jan 18 '22 at 07:24

1 Answers1

-1

I'm afraid you will have to go for converters (don't really know why you don't want some ?)

From this SO question :

The null "item" is not being selected by the keyboard at all - rather the previous item is being unselected and no subsequent item is (able to be) selected.

In short, you can neither select nor deselect a null item in a ComboBox. When you think you are doing so, you are rather deselecting or selecting the previous or a new item.

This can perhaps best be seen by adding a background to the items in the ComboBox. You will notice the colored background in the ComboBox when you select "Hello", but when you deselect it via the keyboard, the background color disappears. We know this is not the null item, because the null item actually has the background color when we drop the list down via the mouse!

The following XAML, modified from that in the original question, will put a LightBlue background behind the items so you can see this behavior.

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox x:Name="bars" Height="21" SelectedItem="{Binding Bar}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="LightBlue" Width="200" Height="20">
                        <TextBlock Text="{Binding Name}" />
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel> </Window> ``` 

If you want further validation, you can handle the SelectionChanged event on the ComboBox and see that "selecting the null item" actually gives an empty array of AddedItems in its SelectionChangedEventArgs, and "deselecting the null item by selecting 'Hello' with the mouse" gives an empty array of RemovedItems.

Nicolas Voron
  • 2,916
  • 1
  • 21
  • 35
  • Not really relevant, the `null` item can be selected in the `ListBox` using the keyboard. As to why I would prefer not using a converter is simple: reduce converter class spam. – ManIkWeet Jan 18 '22 at 07:21
  • 1) your answer details nothing, just that converters should be used (how). 2) To me it seems that having specialized converters (in this case, to handle some nulls) is a bad practise, and you should try and keep only to reusable generic converts. 3) I did not downvote your answer, someone did that before I read it. Out of own experiments I found that `SelectedItem` could be bound, and would set it to `null` when using a keyboard, which contradicts what you quoted. – ManIkWeet Jan 19 '22 at 14:09
  • 1) You are wrong. The answer said that I'm *afraid* there is no other solution, despite the weird behaviour of the framework suggests the opposite. 2) I have no problem with that. But keep in mind that is a personal conviction, not an absolute rule. Just make sure it doesn't cause you more problems than it solves. 3) I reproduce what is in the answer (LightBlue background and `SelectionChangedEventArgs`) on my pc. Maybe this behaviour is configuration-based ? If that's the case, personnaly I'd run away from this kind of problem, since configure each customer dependencies would be a pain. – Nicolas Voron Jan 20 '22 at 08:25