2013-02-28 48 views
0

我目前正在WinRT中编写一个应用程序,我需要从列表视图中的选定项传回一个id给我的viewmodel。我的listview有一个Observable集合作为一个itemsource,因此listview中的每个项目都会有一个不同的id。将数据从选定的项目传递到WinRT中的viewmodel

我的XAML代码类似于这样

<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}" > 
       <WinRtBehaviors:Interaction.Behaviors> 
        <Win8nl_Behavior:EventToCommandBehavior Event="SelectionChanged" 
               Command="DetailsCommand" 
               CommandParameter="{Binding Path=DontKnow, Mode=TwoWay}"/> 
       </WinRtBehaviors:Interaction.Behaviors> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical" > 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock VerticalAlignment="Center" FontWeight="Bold" FontFamily="Segoe UI" Text="{Binding VacationStart, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}' }" Margin="20,0,0,0"></TextBlock> 
           <TextBlock VerticalAlignment="Center" FontWeight="Bold" FontFamily="Segoe UI" Text="{Binding VacationEnd, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}' }" Margin="20,0,0,0"></TextBlock> 
           <TextBlock x:Name="id" VerticalAlignment="Center" FontWeight="Bold" FontFamily="Segoe UI" Text="{Binding VacationRequestId}" Margin="20,0,0,0"></TextBlock> 
          </StackPanel> 
          <TextBlock Text="{Binding StatusView}" Margin="50,0,0,0"></TextBlock> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="Title: " Margin="50,0,0,0"></TextBlock> 
           <TextBlock FontStyle="Italic" Text="{Binding VacationCommentUser}" Margin="5,0,0,0"></TextBlock> 
          </StackPanel> 
         </StackPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 

      </ListView> 

我使用WinRTBehaviors模仿EventToCommand行为,但我不知道我应该如何在我的列表视图得到的项目的某个参数回到我的视图模型。为Mvvm我使用MvvmLight。

回答

3

只需创建一个视图模型的属性然后绑定在你的列表视图的SelectedItem这样的:

SelectedItem={Binding MyProperty, Mode=TwoWay} 

这就是全部。每当用户更改一个值时,您的财产将被更新。

+0

谢谢你这个问题idd,bi不笨,但我想我现在不会忘记:) – Landvis 2013-02-28 09:44:08

0
CommandParameter="{Binding ElementName=MyListBox, Path=SelectedItem}" 

(你必须给你的列表框的x:Name值)

1

绑定所选项目要在房产在你的ViewModel

<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}" SelectedItem="{Binding SelectedVacation, Mode=TwoWay}"> 
1

您应该使用SelectedValuePath提取Id from SelectedItem

<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}" 
          SlectedValuePath="Id" 
          SelectedValue="{Binding SelectedVacationId, Mode=TwoWay}"> 
相关问题