0

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

我的 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。

4

4 回答 4

3

只需在视图模型中创建一个属性,然后将其绑定到列表视图中的 SelectedItem,如下所示:

SelectedItem={Binding MyProperty, Mode=TwoWay}

就这样。每当用户更改值时,您的属性都会更新。

于 2013-02-28T09:34:28.153 回答
1

将所选项目绑定到 ViewModel 中的属性

<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}" SelectedItem="{Binding SelectedVacation, Mode=TwoWay}"> 
于 2013-02-28T09:35:35.370 回答
1

您应该用于从中SelectedValuePath提取:IdSelectedItem

<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}"
                          SlectedValuePath="Id" 
                          SelectedValue="{Binding SelectedVacationId, Mode=TwoWay}"> 
于 2013-02-28T09:43:31.840 回答
0
CommandParameter="{Binding ElementName=MyListBox, Path=SelectedItem}"

(你必须给你的 ListBox 一个 x:Name 值。)

于 2013-02-28T09:34:28.153 回答