";值没有落在期望的范围内”;列出集合,但不在Observable集合中
本文关键字:集合 Observable quot 期望 范围内 | 更新日期: 2023-09-27 18:27:49
这是我的工作代码,
private ObservableCollection<User> _Users;
public ObservableCollection<User> Users
{
get { return _Users; }
set { _Users = value; RaisePropertyChanged(()=> Users); }
}
Users = new ObservableCollection<User>();
for (int i = 1; i <= 10; i++)
{
Users.Add(new User()
{
ADDRESS_LINE_1 = "Test Address",
ADDRESS_LINE_2 = "Test Address 2",
FIRST_NAME = "Test Name " + i,
SURNAME = "Test surname " +i,
DATE_OF_BIRTH = DateTime.Now.Date,
GENDER = "M",
MOBILE_PHONE_NUMBER = "+1100000",
EMAIL_ADDRESS = "Test@email.com",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator.domain.com",
ItemIndex = Users.Count +1
});
}
早些时候我使用
private List<User> _Users;
public List<User> Users
{
get { return _Users; }
set { _Users = value; RaisePropertyChanged(()=> Users); }
}
Users = new List<User>();
for (int i = 1; i <= 10; i++)
{
Users.Add(new User()
{
ADDRESS_LINE_1 = "Test Address",
ADDRESS_LINE_2 = "Test Address 2",
FIRST_NAME = "Test Name " + i,
SURNAME = "Test surname " +i,
DATE_OF_BIRTH = DateTime.Now.Date,
GENDER = "M",
MOBILE_PHONE_NUMBER = "+9100000",
EMAIL_ADDRESS = "Test@email.com",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator1.domain.com",
ItemIndex = Users.Count +1
});
}
当我尝试将其与xaml 中的数据模板绑定时,我不断收到以下异常消息
Value does not fall within the expected range.
堆栈跟踪为空:(
此外,如果我取另一个列表变量并将这些值添加到其中,然后将该列表的浅副本分配给上面的列表,代码会给出我想要的结果,但同样会给出实际情况。
这是数据模板
<DataTemplate>
<Grid Margin="-8,-10,-8,-10" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<StackPanel Orientation="Horizontal">
<!--<Border BorderBrush="Gray" BorderThickness="0.3" Visibility="{Binding IsSelectedItem, Mode=TwoWay, Converter={StaticResource ControlVisibiltyOfSelectedItemInListBox}}">
<TextBlock Text="{StaticResource ArrowGlyph}" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Right"></TextBlock>
</Border>-->
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding SURNAME}"></TextBlock>
</StackPanel>
</Border>
<Border Grid.Column="1" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding FIRST_NAME}"></TextBlock>
</Border>
<Border Grid.Column="2" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" HorizontalAlignment="Stretch">
<Run Text="{Binding ADDRESS_LINE_1}"></Run><LineBreak></LineBreak>
<Run Text="{Binding ADDRESS_LINE_2}"></Run>
</TextBlock>
</Border>
<Border Grid.Column="3" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding DATE_OF_BIRTH}"></TextBlock>
</Border>
<Border Grid.Column="4" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding EMAIL_ADDRESS}"></TextBlock>
</Border>
<Border Grid.Column="5" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding MOBILE_PHONE_NUMBER}"></TextBlock>
</Border>
</Grid>
</DataTemplate>
虽然,Observable系列对我来说很有效,但我想知道为什么List会给我这个例外,我无法弄清楚幕后的实际原因是什么。提前谢谢。
这里唯一让我头疼的是"RaisePropertyChanged
"与ObservableCollection
一起使用,或者至少您必须继承INotifyPropertyChanged
接口。在您的第二个实现列表中,没有获取"RaisePropertyChanged"方法。将列表声明视为:
private List<User> _Users;
public List<User> Users
{
get { return _Users; }
set { _Users = value;}
}
我认为这应该可以解决错误。但是注意只要对象的状态(Added、Removed和Modified)更改到您希望通知基础集合或容器状态已更改的程度,这样做就不会引发PropertyChanged事件。
有关更多信息,请阅读:Silverlight 中的List与ObservableCollection与INotifyPropertyChanged