Question

I am using Coverflow control in Windows 8 C# XAML App.

I am trying to bind the selected Item property of this control to a property but that is not having any effect. Following is my code that i am using do let me know what is the mistake i am doing?

 <controls:CoverFlow
     Height="85"
     Margin="0,40,0,0"
     HorizontalAlignment="Stretch"
     VerticalAlignment="Stretch"
     SpaceBetweenItems="35"
     SelectedItem="{Binding DiarySelectedItem.DiarySmileyId}"
     SpaceBetweenSelectedItemAndItems="40"
     ItemClick="ItemView_ItemClick"
     ItemTemplate="{StaticResource 50ItemTemplate}"
     ItemsSource="{Binding DiarySmileyIdOC}" 
     SelectedItemChanged="CoverFlow_SelectedItemChanged" />
/// <summary>
///     Sets and gets the DiarySelectedItem property.
///     Changes to that property's value raise the PropertyChanged event.
/// </summary>
public DiaryItemDetail DiarySelectedItem
{
    get { return _diarySelectedItem; }

    set
    {
        if (_diarySelectedItem == value)
        {
            return;
        }

        _diarySelectedItem = value;
        RaisePropertyChanged(DiarySelectedItemPropertyName);
    }
}

/// <summary>
///     Gets or sets the Diary Smiley Id Observable Collection.
/// </summary>
/// <value>
///     The Diary Smiley Id Observable Collection.
/// </value>
public ObservableCollection<int> DiarySmileyIdOC
{
    get { return _diarySmileyIdOC; }

    set
    {
        if (_diarySmileyIdOC == value)
        {
            return;
        }

        _diarySmileyIdOC = value;

        // Update bindings, no broadcast
        RaisePropertyChanged(DiarySmileyIdOCPropertyName);
    }
}
Was it helpful?

Solution

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 is null, nothing will happen because of a silent NullReferenceException when assigning to DiarySelectedItem.DiarySmileyId.
  • If DiarySelectedItem is not null and DiaryItemDetail.DiarySmileyId has a public setter, the value of DiarySmileyId property in the instance assigned to DiarySelectedItem will change while the other properties will remain the same.
  • If DiarySelectedItem is not null and DiaryItemDetail.DiarySmileyId doesn't have a public setter, nothing will happen since DiarySmileyId 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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top