4

Xaml 命名空间

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
                        xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                        xmlns:appBar="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
                        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP8"



<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"
      Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>




    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel"
                Grid.Row="0"
                Margin="12,17,0,28">

        <TextBlock Text="Beneficiary"
                   Margin="9,-7,0,0"
                   Style="{StaticResource PhoneTextTitle1Style}" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel"
          Grid.Row="1"
          Margin="12,0,12,0">

        <toolkit:LongListMultiSelector
                           x:Name="beneficoryList"
                            ItemsSource="{Binding BeneficoryCollection}"
                            EnforceIsSelectionEnabled="{Binding DataContext.IsSelectionEnabled, ElementName=LayoutRoot,Mode=TwoWay}">

            <!--Is Selection Enabled Changed Command-->
            <!--<interactivity:Interaction.Triggers>
                <interactivity:EventTrigger EventName="IsSelectionEnabledChanged">
                    <interactivity:InvokeCommandAction Command="{Binding DataContext.IsSelectionEnabledChangedOn,ElementName=LayoutRoot,Mode=OneTime}"/>
                </interactivity:EventTrigger>
            </interactivity:Interaction.Triggers>-->


            <toolkit:LongListMultiSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                        <TextBlock Text="{Binding Number}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
                        <TextBlock Text="{Binding Operator}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
                        <!-- Interaction region-->
                        <interactivity:Interaction.Triggers>
                            <interactivity:EventTrigger EventName="Tap">
                                <interactivity:InvokeCommandAction Command="{Binding TapCommand}" CommandParameter="{Binding }"/>
                            </interactivity:EventTrigger>
                        </interactivity:Interaction.Triggers>
                        <!-- Interaction region ends-->
                    </StackPanel>
                </DataTemplate>
            </toolkit:LongListMultiSelector.ItemTemplate>

        </toolkit:LongListMultiSelector>
    </Grid>
</Grid>  
    <interactivity:Interaction.Behaviors>
        <appBar:ApplicationBarBehavior x:Name="appBar" IsVisible="True"
                                       BackgroundColor="{StaticResource PhoneAccentColor}"
                                       ForegroundColor="{StaticResource PhoneBackgroundColor}" Mode="Default">



            <appBar:ApplicationBarIconButton Text="Add"
                                       IconUri="/Toolkit.Content/ApplicationBar.Add.png"
                                       IsVisible="{Binding IsAddCommandVisible,Mode=TwoWay}"  
                                       Command="{Binding AddCommand}"/>

            <appBar:ApplicationBarIconButton Text="Delete"
                                         IconUri="/Toolkit.Content/ApplicationBar.Delete.png"
                                         Command="{Binding DeleteCommand,Mode=OneTime}"
                                         CommandParameter="{Binding SelectedItems,ElementName=beneficoryList}"/>


            <!--Menu Items-->
            <appBar:ApplicationBarBehavior.MenuItems>
                <appBar:ApplicationBarMenuItem Text="HelpDesk" />
                <appBar:ApplicationBarMenuItem Text="Contact Us"/>
            </appBar:ApplicationBarBehavior.MenuItems>
            <!--Menu Items End-->

        </appBar:ApplicationBarBehavior>

    </interactivity:Interaction.Behaviors>

查看模型

private RelayCommand<object> _ondeleteCommand;

    public RelayCommand<object> DeleteCommand
    {
        get { return _ondeleteCommand ?? (_ondeleteCommand = new RelayCommand<object>(OnDeleteCommand)); }
    }



    private void OnDeleteCommand(object tobeDeleted)
    {
        if (tobeDeleted != null)
        {
           // Delete
        }
    }

如何将 LongListMultiSelector 的 selectedItems 传递给命令。在命令参数中,我总是得到空值。输出窗口中没有绑定错误。

钦巴利诺指南

4

1 回答 1

3
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
                        xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                        xmlns:appBar="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
                        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP8"


 <!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"
      Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--App Bar Binding-->
    <interactivity:Interaction.Behaviors>
        <appBar:ApplicationBarBehavior x:Name="appBar" IsVisible="True"
                                       BackgroundColor="{StaticResource PhoneAccentColor}"
                                       ForegroundColor="{StaticResource PhoneBackgroundColor}" Mode="Default">



            <appBar:ApplicationBarIconButton Text="Add"
                                       IconUri="/Toolkit.Content/ApplicationBar.Add.png"
                                       IsVisible="{Binding IsAddCommandVisible,Mode=TwoWay}"  
                                       Command="{Binding AddCommand}"/>

            <appBar:ApplicationBarIconButton Text="Delete"
                                         IconUri="/Toolkit.Content/ApplicationBar.Delete.png"
                                         Command="{Binding DeleteCommand,Mode=OneTime}"
                                         CommandParameter="{Binding SelectedItems,ElementName=beneficoryList}"/>


            <!--Menu Items-->
            <appBar:ApplicationBarBehavior.MenuItems>
                <appBar:ApplicationBarMenuItem Text="HelpDesk" />
                <appBar:ApplicationBarMenuItem Text="Contact Us"/>
            </appBar:ApplicationBarBehavior.MenuItems>
            <!--Menu Items End-->

        </appBar:ApplicationBarBehavior>

    </interactivity:Interaction.Behaviors>


    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel"
                Grid.Row="0"
                Margin="12,17,0,28">

        <TextBlock Text="Beneficiary"
                   Margin="9,-7,0,0"
                   Style="{StaticResource PhoneTextTitle1Style}" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel"
          Grid.Row="1"
          Margin="12,0,12,0">

        <toolkit:LongListMultiSelector
                           x:Name="beneficoryList"
                            ItemsSource="{Binding BeneficoryCollection}"
                            EnforceIsSelectionEnabled="{Binding DataContext.IsSelectionEnabled, ElementName=LayoutRoot,Mode=TwoWay}">
            <toolkit:LongListMultiSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                        <TextBlock Text="{Binding Number}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
                        <TextBlock Text="{Binding Operator}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
                        <!-- Interaction region-->
                        <interactivity:Interaction.Triggers>
                            <interactivity:EventTrigger EventName="Tap">
                                <interactivity:InvokeCommandAction Command="{Binding TapCommand}" CommandParameter="{Binding }"/>
                            </interactivity:EventTrigger>
                        </interactivity:Interaction.Triggers>
                        <!-- Interaction region ends-->
                    </StackPanel>
                </DataTemplate>
            </toolkit:LongListMultiSelector.ItemTemplate>

        </toolkit:LongListMultiSelector>


    </Grid>


</Grid>

在可视化树中找不到元素“beneficoryList”,我将应用栏放置在网格之外,现在我将应用栏移动到网格内,因此“beneficoryList”现在是 selectedItems 传递给 VM。我认为 WP 只允许遍历其直接父级

于 2015-03-07T17:19:14.537 回答