0

在 Windows 8 C# XAML App 中使用 Coverflow 控件

我正在尝试将此控件的选定 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);
    }
}
4

1 回答 1

0

由于您绑定ObservableCollection<int>ItemsSource,SelectedItem也是int如此。DiarySelectedItem.DiarySmileyId您已经通过绑定到而不是直接绑定到来考虑到这一点DiarySelecteditem

这有一个副作用,尽管您实际上并没有改变DiarySelectedItem,而只是试图改变其DiarySmileyId属性的值。这可能不是您想要的,原因如下:

  • 如果DiarySelectedItem是,则由于分配给时null的无声,什么都不会发生。NullReferenceExceptionDiarySelectedItem.DiarySmileyId
  • 如果DiarySelectedItem不是null并且DiaryItemDetail.DiarySmileyId具有公共设置器,则DiarySmileyId分配给的实例中的属性值DiarySelectedItem将更改,而其他属性将保持不变。
  • 如果DiarySelectedItem不是null并且DiaryItemDetail.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所以你可以直接绑定SelectedItemSelectedItem

于 2013-01-03T05:56:23.293 回答