-
Notifications
You must be signed in to change notification settings - Fork 512
Description
Description:
When using x:Bind to bind a ListView’s ItemsSource to an ObservableCollection, the app crashes at runtime with the error:
Value does not fall within the expected range.
This issue does not occur when binding to a collection of complex objects (e.g., ObservableCollection). It appears to be a limitation or bug in how x:Bind handles collections of primitive types such as strings.
Steps to reproduce:
Create a ViewModel exposing an ObservableCollection property.
In XAML, bind a ListView's ItemsSource using x:Bind to that collection.
Run the app and observe the runtime exception.
public class LanguageControlViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private ObservableCollection<LanguageViewModel> _availableLanguages = new ObservableCollection<LanguageViewModel>();
public ObservableCollection<LanguageViewModel> AvailableLanguages
{
get => _availableLanguages;
set
{
if (_availableLanguages != value)
{
_availableLanguages = value;
OnPropertyChanged(nameof(AvailableLanguages));
}
}
}
public LanguageControlViewModel()
{
AvailableLanguages.Add(new LanguageViewModel { DisplayName = "English" });
AvailableLanguages.Add(new LanguageViewModel { DisplayName = "Italiano" });
}
}
public class LanguageViewModel
{
public string DisplayName { get; set; }
}
public sealed partial class MainPage : Page
{
public LanguageControlViewModel ViewModel { get; set; } = new();
public MainPage()
{
InitializeComponent();
}
}
<Page
x:Class="App1.MainPage"
xmlns:vm="using:App1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Background="Transparent">
<ListView ItemsSource="{x:Bind ViewModel.AvailableLanguages, Mode=OneWay}" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:LanguageViewModel">
<TextBlock Text="{x:Bind DisplayName, Mode=OneWay}" FontSize="18" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
Expected behavior:
The ListView should display the list of strings without throwing exceptions.
Actual behavior:
Runtime exception thrown with message:
Value does not fall within the expected range.
Environment:
Windows 10 / 11
UWP / WinUI 3
Visual Studio version 2022
Additional context:
This appears to be a known limitation documented in multiple places, but it severely limits usage scenarios where collections of simple types are bound using x:Bind. A fix or improved support would be highly appreciated.
Example project
App1.zip