数据绑定CoverFlow Control的Selected Item属性?

时间:2013-01-02 13:32:26

标签: c# xaml windows-8 windows-runtime coverflow

我正在使用Coverflow control in Windows 8 C# XAML App

我正在尝试将此控件的选定Item属性绑定到属性,但这没有任何效果。以下是我正在使用的代码,让我知道我在做什么错误?

 <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);
    }
}

1 个答案:

答案 0 :(得分:0)

由于您将ObservableCollection<int>绑定到ItemsSourceSelectedItem也是int。您已通过绑定到DiarySelectedItem.DiarySmileyId而非直接绑定到DiarySelecteditem来考虑此问题。

这有副作用,但您实际上并未更改DiarySelectedItem,而只是尝试更改其DiarySmileyId属性的值。由于以下几个原因,这可能不是您想要的:

  • 如果DiarySelectedItemnull,则在分配给NullReferenceException时,由于无声DiarySelectedItem.DiarySmileyId,不会发生任何事情。
  • 如果DiarySelectedItem不是nullDiaryItemDetail.DiarySmileyId有公共设置器,则分配给DiarySmileyId的实例中DiarySelectedItem属性的值将会更改其他属性将保持不变。
  • 如果DiarySelectedItem不是nullDiaryItemDetail.DiarySmileyId没有公开制定者,则无法分配DiarySmileyId,因此不会发生任何事情。

您应该有一个单独的DiarySelectedItemId属性来实现所需的行为:

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);
    }
}

为什么你没有将ObservableCollection<DiaryItemDetail>设置为ItemsSource,所以你可以将SelectedItem直接绑定到SelectedItem