zoukankan      html  css  js  c++  java
  • 在WindowsPhone8中生成基于MVVM Light的LongListSelector命令绑定

    继续《在WindowsPhone8中生成基于MVVM Light的LongListSelector拼音检索绑定》一文,项目不再引用上一个项目,但功能实现相同,我们已经对数据作了绑定,下面要携带参数传值导航至具体内容页:

    1.在引用中添加

        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"
    

     2.绑定命令:我实现的功能为:单机LongListSelector的某个项(此例中为文本框),跳转至相应页面(ContentPage)内容页

    <phone:LongListSelector
                       x:Name="MountainIndexLLS"
                       JumpListStyle="{StaticResource AddrBookJumpListStyle}"
                       Background="Transparent"
                       GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"        
                       LayoutMode="List"
                       IsGroupingEnabled="true"
                       HideEmptyGroups ="true" ItemsSource="{Binding DataSource}">
                        <phone:LongListSelector.ItemTemplate>
                            <DataTemplate>
                                <StackPanel VerticalAlignment="Top">
                                    <TextBlock FontWeight="Bold"  Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Foreground="White"/>
                                    <TextBlock Text="{Binding Region}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Foreground="Gray"/>
                                </StackPanel>
                            </DataTemplate>  
                        </phone:LongListSelector.ItemTemplate>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Tap">
                                <cmd:EventToCommand Command="{Binding TapCommand}" CommandParameter="{Binding SelectedItem,ElementName=MountainIndexLLS}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </phone:LongListSelector>
    

     在这里,我们将上例中的LongListSelector中LongListSelector.ItemTemplate的样式代码由PhoneApplicationPage.Resources中转移到了LongListSelector中,在这里,每一个LongListSelector项均被绑定了一个TapCommand的命令,下面,我们在MainViewModel中完成这条命令,实现我们需要的功能。

    3.写命令代码:(MainViewModel)

    public RelayCommand<MountainIndex> TapCommand { get; private set; }
            public void NavigationTap(MountainIndex mouIndex)
            {
                System.Windows.MessageBox.Show(mouIndex.Name+mouIndex.Region);
            }
    

     我们先不实现导航,仅仅是取得文本框的内容

    public MainViewModel()
            {
                TapCommand = new RelayCommand<MountainIndex>((mouIndex) => NavigationTap(mouIndex));
            }
    

     实现结果如下:

  • 相关阅读:
    BZOJ1691: [Usaco2007 Dec]挑剔的美食家
    BZOJ1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
    BZOJ3057: 圣主的考验
    BZOJ1770: [Usaco2009 Nov]lights 燈
    1710: [Usaco2007 Open]Cheappal 廉价回文
    「Poetize7」电话线路
    「Poetize6」Candle
    「Poetize5」水叮当的舞步
    解题:CF983A Finite or not
    解题:POI 2013 Triumphal arch
  • 原文地址:https://www.cnblogs.com/valentineisme/p/3088114.html
Copyright © 2011-2022 走看看