我正在使用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);
}
}
答案 0 :(得分:0)
由于您将ObservableCollection<int>
绑定到ItemsSource
,SelectedItem
也是int
。您已通过绑定到DiarySelectedItem.DiarySmileyId
而非直接绑定到DiarySelecteditem
来考虑此问题。
这有副作用,但您实际上并未更改DiarySelectedItem
,而只是尝试更改其DiarySmileyId
属性的值。由于以下几个原因,这可能不是您想要的:
DiarySelectedItem
为null
,则在分配给NullReferenceException
时,由于无声DiarySelectedItem.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
,所以你可以将SelectedItem
直接绑定到SelectedItem
?