【问题标题】:Passing data from selected item to viewmodel in WinRT将数据从选定项目传递到 WinRT 中的视图模型
【发布时间】:2013-02-28 09:21:08
【问题描述】:

我目前正在 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。

【问题讨论】:

    标签: c# xaml mvvm-light winrt-xaml


    【解决方案1】:

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

    SelectedItem={Binding MyProperty, Mode=TwoWay}
    

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

    【讨论】:

    • 谢谢你,这是我的问题,有点愚蠢,但我想我现在不会忘记 :)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您应该使用SelectedValuePathSelectedItem 中提取Id

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

      【讨论】:

        【解决方案4】:
        CommandParameter="{Binding ElementName=MyListBox, Path=SelectedItem}"
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多