我在嘗試從我的「列表」屏幕中傳遞列表的selectedIndex時出現問題。從Databound列表框中傳遞對象而不是selectedIndex
在我的清單屏幕上,我有以下的結合:
後臺代碼:
lbPrograms.ItemsSource = App.ViewModel.Items;
XAML:
<ListBox x:Name="lbPrograms" ItemsSource="{Binding Items}" SelectionChanged="lbPrograms_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<Image x:Name="ItemImage" Source="/images/ArrowImg.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding programName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="DetailsText" Text="{Binding createDate}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock x:Name="programId" Text="{Binding programId}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
<!--<Image x:Name="ItemFavs" Source="/images/favs.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<Image x:Name="ItemDelete" Source="/images/delete.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在我的細節畫面我仰望使用selectedIndex值:
App.ViewModel.Refresh();
DataContext = App.ViewModel.Items[index].nValDictionary;
但是,由於我無法弄清楚如何更新我的iEnumerable集合中的值,我一直在removeAt [index]然後重新添加到集合中。所以任何人都可以告訴我,如何使用更新我的集合中的現有元素?如果沒有,我可以傳遞programId(即在我的綁定中)而不是selectedIndex,因爲索引在刪除/添加功能後全部搞砸了。
請指教。
UPDATE:
在幾個論壇上發言後,我要澄清,我在我在我的對象屬性實現INotifyChanged事件。
基本上下面的代碼片段是我目前的問題:
private void FavBtn_Click(object sender, EventArgs e)
{
foreach (var item in App.ViewModel.Items)
{
if ((item.currentProgram == true) && (item.programId != index))
{
item.currentProgram = false;
}
if (item.programId == index)
{
item.currentProgram = true;
}
}
當我運行這一切看起來是好的。
但是,當我導航到另一個頁面時,我重新加載對象並且更改丟失。這幾乎就像我需要在導航前保存它們,但是,如果我做一個item.Save();我的清單中有重複的內容。
@米克 - 感謝您的漫步。其實我在我的課上實現了INotifyPropertyChanged,然而,在我跟着他們有一個保存事件的例子中...(http://onishimura.com/2010/07/25/windows-phone-7-tutorial -create-a-simple-notes-app-with-silverlight-part-1 /)每次我打電話給。保存在課堂上我得到一個新的項目,而不是隻是一個更新的項目。我將重新編寫我的課程來模仿上述示例中的內容,看看是否有所作爲。謝謝! – webdad3 2010-12-13 05:13:11
如果需求是能夠添加項目,而不是更新列表中的項目,您應該使用ObservableCollection爲您的集合..這基本上只是INotifyPropertyChanged的集合。 – 2010-12-13 06:15:16
@Mick ...與什麼?我沒有按照你想告訴我的內容。我感謝你試圖幫助我。 – webdad3 2010-12-13 17:45:02