The binding Microsoft ListBox or Telerik ListBox crush application because it set CurrentPosition to wrong value when collection is empty. I hope this problem will be fixed pretty soon.
This binding raise exception in ItemsControlSelector (see bug1.png)
Binding to Microsoft ListBox expose source of the problem in QueryableCollectionView (see bug2.png)
Code for model taken from Telerik WPF example
This binding raise exception in ItemsControlSelector (see bug1.png)
<
telerik:RadListBox
x:Name
=
"ContentItemsControl"
ItemTemplate
=
"{StaticResource GoalSimpleViewTemplate}"
ItemsSource
=
"{Binding Path=ItemsSource, ElementName=MainControl }"
SelectedItem
=
"{Binding Path=SelectedItem, ElementName=MainControl }"
>
</
telerik:RadListBox
>
Binding to Microsoft ListBox expose source of the problem in QueryableCollectionView (see bug2.png)
<
ListBox
x:Name
=
"ContentItemsControl"
ItemTemplate
=
"{StaticResource GoalSimpleViewTemplate}"
ItemsSource
=
"{Binding Path=ItemsSource, ElementName=MainControl }"
SelectedItem
=
"{Binding Path=SelectedItem, ElementName=MainControl }"
>
</
ListBox
>
Code for model taken from Telerik WPF example
public
class
MainViewModel : BaswViewModel, INavigationAware
{
private
QueryableDataServiceCollectionView<Goal> _goals;
private
Goal _selectedGoal;
private
bool
_isGoalsBusy =
true
;
public
MainViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IConnectionService connectionController, IOptionsService optionsService)
:
base
(eventAggregator, regionManager, connectionController, optionsService)
{
}
public
IEnumerable<Goal> Goals
{
get
{
return
IsGoalsBusy ?
null
: _goals; }
}
public
bool
IsGoalsBusy
{
get
{
return
_isGoalsBusy; }
set
{
if
(_isGoalsBusy != value)
{
_isGoalsBusy = value;
RaisePropertyChanged(
"IsGoalsBusy"
);
}
}
}
public
Goal SelectedGoal
{
get
{
return
_selectedGoal; }
set
{
if
(_selectedGoal != value)
{
_selectedGoal = value;
RaisePropertyChanged(
"SelectedGoal"
);
}
}
}
public
bool
IsNavigationTarget(NavigationContext navigationContext)
{
SelectedGoal =
null
;
SelectedActivity =
null
;
return
true
;
}
public
void
OnNavigatedFrom(NavigationContext navigationContext)
{
}
public
void
OnNavigatedTo(NavigationContext navigationContext)
{
}
protected
override
void
OnModelChanged()
{
base
.OnModelChanged();
if
(Context !=
null
)
{
UIDispatcher.BeginInvoke(() => { Initialize(); });
}
}
private
void
Initialize()
{
_goals =
new
QueryableDataServiceCollectionView<Goal>(Context, Context.Goals);
_goals.PropertyChanged +=
this
.OnGoalsViewPropertyChanged;
_goals.LoadedData +=
this
.OnGoalsViewLoadedData;
_goals.PageSize = 10;
_goals.AutoLoad =
true
;
RaisePropertyChanged(
"Goals"
);
}
private
void
OnGoalsViewPropertyChanged(
object
sender, PropertyChangedEventArgs e)
{
if
(e.PropertyName ==
"IsBusy"
)
{
this
.IsGoalsBusy = _goals.IsBusy;
}
}
private
void
OnGoalsViewLoadedData(
object
sender, LoadedDataEventArgs e)
{
if
(e.HasError)
{
e.MarkErrorAsHandled();
}
}
}
}