I'm working on my first WP7 App and this problem causes me some headache.
I've a ListBox defined like this
<ListBox Grid.Row="1" ItemsSource="{Binding MyItemList}" SelectedItem="{Binding MySelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="35" />
<TextBlock Text="{Binding Details}" FontSize="15"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Binding the ItemsSource works fine, but the MySelectedItem-Property doesn't get updated when selecting an item. Is this function not implemented (like in WPF) or am I just doing something? :-)
Just use -
SelectedItem="{Binding MySelectedItem, Mode=TwoWay}"
and it should be fine.
您可能必须为ListBox设置IsSynchronizedWithCurrentItem="True"
。
This is my workaround.. I hope someone will post a more elegant solution.
XAML:
<ListBox Name="DecksListBox" ItemsSource="{Binding MyItemsList}"
SelectionChanged="UpdateSelectedItem"
Code-behind:
private void UpdateSelectedItem(object sender, SelectionChangedEventArgs e)
{
// Ignore if there is no selection
if (DecksListBox.SelectedIndex == -1)
return;
App.ViewModel.MySelectedItem = App.ViewModel.MyItemsList[DecksListBox.SelectedIndex];
}
Maybe this might help someone in the meantime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.