這是我工作的代碼,「值不在預期範圍」列表集合,而不是在觀察集合
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 = "[email protected]",
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 = "[email protected]",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator1.domain.com",
ItemIndex = Users.Count +1
});
}
,我不斷收到以下異常消息,當我嘗試將它與xaml中的數據模板綁定時
Value does not fall within the expected range.
堆棧跟蹤爲空:(
另外,如果我把另一個數組變量,並將這些值添加到它,然後分配該列表到我上面所列內容的淺表副本,代碼給我想要的結果,但同樣究竟繞來繞去。
這是DataTemplate中
<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>
雖然,觀察到的集合爲我工作,但我很奇怪,爲什麼清單給我這個例外,我無法找出什麼是幕後的實際原因。提前致謝。
你在哪裏得到錯誤? – Ehsan
將該集合綁定到我在xaml中的數據模板。 @卡爾·安德森 - MOBILE_PHONE_NUMBER是字符串 –
你能寫一個很小的項目,可以重現這個問題?我無法使用您的發佈代碼進行復制,只要綁定和模板被正確書寫,它就可以與列表一起使用。 –
kennyzx