Since you're binding ObservableCollection<int>
to ItemsSource
, SelectedItem
is int
as well. You've taken this into account by binding to DiarySelectedItem.DiarySmileyId
instead of directly to DiarySelecteditem
.
This has a side effect though that you're not actually changing DiarySelectedItem
but only trying to change the value of its DiarySmileyId
property. This is probably not what you want for several reasons:
- If
DiarySelectedItem
isnull
, nothing will happen because of a silentNullReferenceException
when assigning toDiarySelectedItem.DiarySmileyId
. - If
DiarySelectedItem
is notnull
andDiaryItemDetail.DiarySmileyId
has a public setter, the value ofDiarySmileyId
property in the instance assigned toDiarySelectedItem
will change while the other properties will remain the same. - If
DiarySelectedItem
is notnull
andDiaryItemDetail.DiarySmileyId
doesn't have a public setter, nothing will happen sinceDiarySmileyId
can't be assigned to.
You should have a separate DiarySelectedItemId
property to achieve the desired behavior:
public DiaryItemDetail DiarySelectedItemId
{
get { return _diarySelectedItem.DiarySmileyId; }
set
{
if (_diarySelectedItem.DiarySmileyId == value)
{
return;
}
DiarySelectedItem = GetDairyItemDetail(value); // retrieve instance from repository
RaisePropertyChanged(DiarySelectedItemIdPropertyName);
}
}
public DiaryItemDetail DiarySelectedItem
{
get { return _diarySelectedItem; }
set
{
if (_diarySelectedItem == value)
{
return;
}
_diarySelectedItem = value;
RaisePropertyChanged(DiarySelectedItemPropertyName);
// notification to change the selected item in Coverflow if selected item is changed in code
RaisePropertyChanged(DiarySelectedItemIdPropertyName);
}
}
How come you're not setting ObservableCollection<DiaryItemDetail>
to ItemsSource
, so you could bind SelectedItem
directly to SelectedItem
?