Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListBoxItem.IsSelected Binding does not work anymore with preview5 #10249

Closed
llfab opened this issue Feb 7, 2023 · 6 comments
Closed

ListBoxItem.IsSelected Binding does not work anymore with preview5 #10249

llfab opened this issue Feb 7, 2023 · 6 comments

Comments

@llfab
Copy link

llfab commented Feb 7, 2023

Describe the bug
I am binding a ListBoxItem's IsSelected property to my data model of the item as follows:

          <ListBox SelectionMode="Single, Toggle" 
                   Items="{TemplateBinding Items}" 
                   SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource TemplatedParent}}"
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                   ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ListBox.Styles>
              <Style Selector="ListBoxItem">
                <Setter Property="IsEnabled" Value="{Binding IsSelectable}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                ...

Originally I even had

<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />

to receive the changed selection directly in the items data model.

This has worked in preview4 but is not working anymore in preview5. As I checked in the Debugger, the IsEnabled property works nicely and the property getter in the data model is called. For IsSelected neither the getter nor the setter (if used with TwoWay) is called.

To Reproduce
as above

Expected behavior
Getter and setter for the data model property should be called

Desktop (please complete the following information):

  • OS: Windows 10
  • Version [11.0.0-preview5]
@grokys
Copy link
Member

grokys commented Feb 14, 2023

This is no longer working because it's dependent on the order in which the style is applied in respect to the ListBox setting the IsSelected property. Before #8600, the style was applied before SelectingItemsControl.MarkContainerSelected was called but now they're called in the reverse order.

Not sure what we should do here. If we want this to work, we'd have to think about how best to do it without being dependent on in which order IsSelected is set. But do we want to explicitly support this use-case? We already have a few ways to get multiple selection status from the view model layer to the view layer:

  • SelectingItemsControl.Selection
  • SelectingItemsControl.SelectedItems

Is there any reason you can't use any of the existing explicitly supported mechanisms @llfab ?

@llfab
Copy link
Author

llfab commented Feb 14, 2023

No, there is no reason. In fact, we already changed to SelectedItem. I believe the IsSelected was a good fit to the VM/M implementation that we used at the time but it was just a minor change to go to your proposed way. If IsSelected cannot or shall not be used anymore, maybe that is worth mentioning it in docu.

@llfab
Copy link
Author

llfab commented Feb 14, 2023

If IsSelected creates "order problems" I would actually not support that. SelectedItem(s) will always be sufficient.

@grokys
Copy link
Member

grokys commented Feb 14, 2023

Ok, great. Yeah I'll add that to the list of things we need to document for 11.0.

@grokys grokys closed this as completed Feb 14, 2023
@llfab
Copy link
Author

llfab commented Feb 14, 2023

However, IsSelected within the control (just not as binding) does work and should continue to work because you need that to apply the correct style, right?

@grokys
Copy link
Member

grokys commented Mar 22, 2023

Going to reopen this as it's going to cause problems as people expect this to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants