Windows phone 7 使用MVVM从列表框中获取selecteditem

Windows phone 7 使用MVVM从列表框中获取selecteditem,windows-phone-7,mvvm-light,Windows Phone 7,Mvvm Light,我在这个项目中使用MVVM,我有一个绑定到客户集合的列表框。我想创建一个事件,使用elementselected的id导航detailsPage: <ListBox ItemsSource="{Binding Customers}" x:Name="state_list" SelectionChanged="state_list_SelectionChanged"> <i:Interaction.Triggers>

我在这个项目中使用MVVM,我有一个绑定到客户集合的列表框。我想创建一个事件,使用elementselected的id导航detailsPage:

 <ListBox ItemsSource="{Binding Customers}" x:Name="state_list" SelectionChanged="state_list_SelectionChanged">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="selectionchanged">
                    <cmd:EventToCommand Command="{Binding stateSelectedCommand}" PassEventArgsToCommand="True"  />

                 </i:EventTrigger>
            </i:Interaction.Triggers>
                <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding nom}" />
                        <!--TextBlock Text="{Binding LastName}" />
                        <TextBlock Text="{Binding Text, ElementName=tbCount}" /-->
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我不知道如何获取所选项目以将其添加到uri,然后使用它获取数据。一个例子或教程会很有帮助。谢谢:)

我会在ViewModel中创建一个“SelectedCustomer”属性(在您的Customers属性旁边),并将其绑定到SelectedItem。然后,在该属性的setter上,您可以导航到所需的页面。这样可以消除混乱的事件和命令

<ListBox x:Name="state_list 
         ItemsSource="{Binding Customers}" 
         SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}">

AlexDrenea提供了一种将SelectedItem绑定到viewmodel上的属性的好方法。如果您希望在MVVM体系结构中基于此进行导航,我建议使用消息传递来完成

我在前一段时间写的一篇博文中谈到了这一点,但在MVVMLight中做这件事的简短总结是创建一个位于应用程序级别的Navigator类

public class Navigator
    {

        private PhoneApplicatoinFrame RootFrame;

        public Navigator(PhoneApplicationFrame frame)
        {
            RootFrame = frame;
            RegisterMessages();
        }

        private void RegisterMessages()
        {
            Messenger.Default.Register<ShowTrackerMessage>(this, ShowTracker);
        }

        private void ShowTracker(ShowTrackerMessage msg)
        {
            RootFrame.Navigate(new Uri("/Views/ItemLocationCompassView.xaml", UriKind.RelativeOrAbsolute));
        }

}

然后,您可以选择如何发送导航消息

选项1:在ViewModel中,挂接PropertyChanged事件(INotifyPropertyChanged的一部分),并在SelectedItem属性更改时发送相应的消息

选项2:连接到列表框的SelectionChanged事件。我使用MVVMLight的EventToCommand将该事件发送到ViewModel中的RelayCommand,然后做出适当的反应,将消息发送到Navigator对象


我在:

中详细介绍了这一点,并确保“SelectedItem”使用双向绑定。我以前使用过这种方法,但它似乎有副作用——例如,您正在将特定于手机的逻辑合并到视图模型中,因此它不再具有便携性。其次,您没有扩展合同,当您设置SelectedCustomer时,您将进入一个包含客户详细信息的新页面,并且需要检查nulltoo@vidalsasoon,仅当计划基于ViewModel更新视图中的选择时,才需要双向绑定。因为原始问题没有提到这一点,所以我没有将其包括在内。更新了代码以检查null。你可以用这样一种断开连接的方式来编写导航方法。如果我真的不理解你的想法,我必须创建一个navigator类,并在每次需要实现此操作时定义目标页面!这不是一个好办法不?除此之外,我想恢复该id并将其放在另一个web请求中以获取数据..我知道我必须使用onNavigatedTO,但如何将其传递到detailsViewModel!谢谢:)您可以将目标页面定义为命令参数,并将其传递给导航器,从而将导航移动到更具声明性的位置。不过,我喜欢我的方法,因为我所有的导航都是集中的,所以如果我必须重新安排或重命名内容,我只有一个地方可以更改它们。
public class Navigator
    {

        private PhoneApplicatoinFrame RootFrame;

        public Navigator(PhoneApplicationFrame frame)
        {
            RootFrame = frame;
            RegisterMessages();
        }

        private void RegisterMessages()
        {
            Messenger.Default.Register<ShowTrackerMessage>(this, ShowTracker);
        }

        private void ShowTracker(ShowTrackerMessage msg)
        {
            RootFrame.Navigate(new Uri("/Views/ItemLocationCompassView.xaml", UriKind.RelativeOrAbsolute));
        }

}
    private static Navigator _navigator;
    public static Navigator Nav
    {
        get { return _navigator; }
    }
_navigator = new Navigator(this.RootFrame);