长列表选择器中的列表框命令未触发-Windows Phone 8-MVVM

沙菲克·阿巴斯(Shafiq Abbas)

我正在创建自定义电话簿,该电话簿读取电话簿联系人并显示在我的应用程序中。所以我要创建一个长列表选择器,并在其中创建一个列表框。

我的列表框将包含电话联系人姓名和电话号码列表,并在特定名称下方带有复选框。我在列表框内编写了一个事件触发器,以跟踪复选框是否被单击。

问题:视图模型中未触发事件。我怀疑由于列表框存在于另一个列表(长列表选择器)中,因此事件未触发。

这是xaml代码:

<phone:LongListSelector Grid.Row="3" LayoutMode="List" ItemsSource="{Binding PhoneBookDataSource}" IsGroupingEnabled="True" HideEmptyGroups="True">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
            <StackPanel Orientation="Vertical" >
                    <TextBlock Text="{Binding PhoneContactName}" FontWeight="SemiBold" FontSize="36" Foreground="Green"></TextBlock>
                    <ListBox ItemsSource="{Binding LstPhoneContactNumber,Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                        <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Tap">
                                    <i:InvokeCommandAction Command="{Binding PhoneNumberCheckedStateChangeCommand}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Width="480">
                                    <TextBlock Text="{Binding PhoneNumberItem}" FontSize="25" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Gray"></TextBlock>
                                    <CheckBox Foreground="Black" Background="Black" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="{Binding IsPhoneNumberItemChecked,Mode=TwoWay}"></CheckBox>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
            </StackPanel>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
        <phone:LongListSelector.GroupHeaderTemplate>
            <DataTemplate>
                <Border Background="Transparent" Padding="5">
                    <Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="2" Width="62" 
     Height="62" Margin="0,0,18,0" HorizontalAlignment="Left">
                        <TextBlock Text="{Binding Key}" Foreground="{StaticResource PhoneForegroundBrush}" FontSize="48" Padding="6" 
        FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                    </Border>
                </Border>
            </DataTemplate>
        </phone:LongListSelector.GroupHeaderTemplate>
        <phone:LongListSelector.JumpListStyle>
            <Style TargetType="phone:LongListSelector">
                <Setter Property="GridCellSize"  Value="113,113"/>
                <Setter Property="LayoutMode" Value="Grid" />
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Width="113" Height="113" Margin="6" >
                                <TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Padding="6" 
           Foreground="{Binding Converter={StaticResource ForegroundConverter}}" VerticalAlignment="Center"/>
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </phone:LongListSelector.JumpListStyle>
    </phone:LongListSelector>

这是我的视图模型:

public DelegateCommand PhoneNumberCheckedStateChangeCommand { get; set; }
    public DelegateCommand SendSMSCommand { get; set; }

public CustomPhoneBookViewModel(INavigationService nav, IDataService data, IAESEnDecrypt encrypt, IGeoLocationService geoLocation, IMessageBus msgBus, ISmartDispatcher smartDispatcher)
        : base(nav, data, encrypt, geoLocation, msgBus, smartDispatcher)
    {
        IsProgressBarBusy = true;
        PhoneContactsList = new ObservableCollection<PhoneBookEntity>();
        PhoneBookDataSource = new ObservableCollection<LLSAlphaKeyGroup<PhoneBookEntity>>();
        InitializeDelegateCommands();
        GetDeviceResolution();
        ReadPhoneBook();
    }

private void OnPhoneNumberItemCheckedStateChangedCommand()
    {
        try
        {
            foreach (var parentItem in PhoneBookDataSource)
            {
                foreach (var childItem in parentItem)
                {
                    foreach (var item in childItem.LstPhoneContactNumber)
                    {
                        if (item.IsPhoneNumberItemChecked)
                            IsSendSMSButtonEnabled = true;
                        return;
                    }
                }
                IsSendSMSButtonEnabled = false;
            }
        }
        catch { }
        finally
        {
            SendSMSCommand.RaiseCanExecuteChanged();
        }
    }

任何建议表示赞赏!

安德烈斯克

使嵌套的ListBox交互绑定到全局ViewModel(而不是其自身的nested DataContext的最简单方法是为最远的LongListSelector分配一个唯一的名称:

<phone:LongListSelector x:Name="OuterList" Grid.Row="3" LayoutMode="List" ItemsSource="{Binding PhoneBookDataSource}" IsGroupingEnabled="True" HideEmptyGroups="True">

并将Command显式绑定到此元素的DataContext(即全局ViewModel):

<i:InvokeCommandAction Command="{Binding ElementName=OuterList, Path=DataContext.PhoneNumberCheckedStateChangeCommand}" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Windows Phone 8中的长列表选择器项中获取文本?

Windows Phone 8中的列表选择器

Windows Phone 8中未显示列表框数据和图像

在Windows Phone 8和MVVM中向应用程序栏动态添加按钮

在mvvm模型中实施Windows Phone应用

从sqlite数据库中选择数据并将值绑定到Windows Phone 8应用中的列表框项

即使在mvvm Windows Phone应用程序中返回后,列表框也会突出显示选定的项目

需要在Windows Phone 8中创建带有动画的列表框删除项

为什么比Windows Phone 8中的列表框更喜欢LongListSelector

从Windows Phone 8的列表框中访问特定的文本块

如何在Windows Phone 8中制作不可滚动的列表框

如何在Windows Phone 8中使用没有列表框的xml文件?

Windows Phone 8中的NavigationDrawer

Windows Phone 8中的getrequeststream

使用MVVM在Windows Phone上从列表拖放到画布

Windows Phone中的列表选择器选择问题

如何阅读列表<>?Windows Phone 8

Windows Phone 8.1中的指南针COMException OnPropertyChanged MVVM

数据绑定SQLIte到列表框(包含三个控件)Windows Phone 8

什么控件用作Windows Phone 8上的内联列表框?

如何在Windows Phone 8的字符串数组中的每个列表框项目上应用背景色?

Windows Phone 8:如何使用MVVM加载每个数据透视表项的内容?

Windows Phone列表框选择器,单击鼠标左键

where命令,XML Feed Windows phone 8

Windows Phone 8消息框问题

如何在Windows Phone 8中更改列表框内的按钮背景图像

在Windows Phone 8中共享应用

在Windows Phone 8中解析xml数据

在Windows Phone 8中获取市民地址