CommunityToolkit / CommunityToolkit/Windows

Using two-way binding with Segmented results in NullReferenceException

Aperta
#843 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
bug components::controls::segmented
Lingua principale
C#
Stelle
1.1k
Fork
166
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### Describe the bug

Attempting to use the Segmented control with a two-way data binding like this

```xml

```
will result in an immediate NullReferenceException when the control loads
```
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.CompilerServices.CastHelpers.Unbox(MethodTable* toTypeHnd, Object obj)
at SegmentedBindingBug.MainWindow.MainWindow_obj1_Bindings.UpdateTwoWay_2_SelectedItem() in C:\Users\Joel\source\issue-reports\SegmentedBindingBug\SegmentedBindingBug\obj\x64\Debug\net10.0-windows10.0.19041.0\win-x64\MainWindow.g.cs:line 201
```

If instead of using a `Mode=TwoWay` binding I use `Mode=OneWay` and listen to the SelectionChanged event and manually sync changes back to the view model, I see that there are four events when the control loads, and one of them has the SelectedValue as null
> Selected value is Foo
> Selected value is null
> Selected value is Foo
> Selected value is Foo

### Steps to reproduce

```text
https://github.com/joelspadin/CommunityToolkitSegmentedBindingBug

1. Create a view model class with an enum property. (I am using CommunityToolkit.Mvvm in this project, but the same happens if I implement INotifyPropertyChanged manually.)
2. Create a `Segmented` with its ItemsSource set to the enum type using the `EnumValues` extension.
3. Bind its `SelectedItem` to the view model property with `Mode=TwoWay`.
```

### Expected behavior

The control's SelectedItem is not null, so using a two-way data binding is possible.

### Screenshots

_No response_

### Code Platform

- [ ] UWP
- [x] WinAppSDK / WinUI 3
- [ ] Web Assembly (WASM)
- [ ] Android
- [ ] iOS
- [ ] MacOS
- [ ] Linux / GTK

### Windows Build Number

- [ ] Windows 10 1809 (Build 17763)
- [ ] Windows 10 1903 (Build 18362)
- [ ] Windows 10 1909 (Build 18363)
- [ ] Windows 10 2004 (Build 19041)
- [ ] Windows 10 20H2 (Build 19042)
- [ ] Windows 10 21H1 (Build 19043)
- [ ] Windows 10 21H2 (Build 19044)
- [ ] Windows 10 22H2 (Build 19045)
- [ ] Windows 11 21H2 (Build 22000)
- [ ] Windows 11 22H2 (Build 22621)
- [ ] Windows 11 23H2 (Build 22631)
- [ ] Windows 11 24H2 (Build 26100)
- [x] Windows 11 25H2 (Build 26200)
- [ ] Windows 11 26H1 (Build 28000)
- [ ] Other (specify)

### Other Windows Build number

_No response_

### App minimum and target SDK version

- [x] Windows 10, version 1809 (Build 17763)
- [ ] Windows 10, version 1903 (Build 18362)
- [ ] Windows 10, version 1909 (Build 18363)
- [x] Windows 10, version 2004 (Build 19041)
- [ ] Windows 10, version 2104 (Build 20348)
- [ ] Windows 11, version 22H2 (Build 22000)
- [ ] Windows 11, version 24H2 (Build 26100)
- [ ] Windows 11, version 25H2 (Build 26200)
- [ ] Windows 11, version 26H1 (Build 28000)
- [ ] Other (specify)

### Other SDK version

_No response_

### Visual Studio Version

2026

### Visual Studio Build Number

18.4.3

### Device form factor

Desktop

### Additional context

This is potentially related to https://github.com/microsoft/microsoft-ui-xaml/issues/3268

### Help us help you

Yes, but only if others can assist.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia con la riproduzione collegata di CommunityToolkitSegmentedBindingBug e lo stack trace generato di MainWindow.g.cs, quindi traccia il binding bidirezionale di SelectedItem e l’inizializzazione della selezione del controllo Segmented. Esegui la riproduzione WinAppSDK/WinUI 3 per confermare l’eccezione durante il caricamento e la selezione null transitoria. Il lavoro è completato quando il controllo associato all’enum viene caricato senza l’eccezione o un SelectedItem temporaneamente null.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
desktop
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.