代码之家  ›  专栏  ›  技术社区  ›  Martin Plante

对列表框选择更改执行页面导航的正确方法是什么

  •  2
  • Martin Plante  · 技术社区  · 14 年前

    这个工具包的一个大问题是,它迫使您在使用MVVM之前通过其他来源学习MVVM,而不是在框架、附带的示例和文档中向您展示MVVM的远景。有没有展示不同概念的样品?拜托,不要录像。

    3 回复  |  直到 14 年前
        1
  •  1
  •   Matt Casto    14 年前

    您是否尝试过修改ListBox ItemTemplate,使每个项都成为一个超链接按钮,并将NavigateURI属性设置为要导航到的页面?

        2
  •  1
  •   tig    14 年前

    <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}"
        SelectionChanged="MainListBox_SelectionChanged" 
        SelectedItem="{Binding Path=SelectedListItem, Mode=TwoWay}">
        <ListBox.ItemTemplate>
             <DataTemplate>
                  <StackPanel Margin="0,0,0,17" Width="432">
                      <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                      <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                  </StackPanel>
            </DataTemplate>
       </ListBox.ItemTemplate>
    </ListBox>
    

    首先,根据上面的绑定到Listbox的SelectedItem属性,并对ViewModel中的属性进行双向绑定( SelectedListItem

    然后在此页的代码隐藏中实现MainListBox的处理程序\u SelectionChanged:

        // Handle selection changed on ListBox
        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;
    
            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsPage.xaml", UriKind.Relative));
    
        }
    

    这是您在主视图中需要的唯一代码。

        public const string SelectedListItemPropertyName = "SelectedListItem";
        private ItemViewModel _SelectedListItem;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public ItemViewModel SelectedListItem
        {
            get
            {
                return _SelectedListItem;
            }
            set
            {
                if (value != _SelectedListItem)
                {
                    _SelectedListItem = value;
                    RaisePropertyChanged(SelectedListItemPropertyName);
                }
            }
        }
    

    public DetailsPage()
    {
        InitializeComponent();
        if (DataContext == null)
            DataContext = App.ViewModel.SelectedListItem;
    
    }
    

    希望这有帮助。

        3
  •  1
  •   earthling    12 年前

    最终,您将需要做的不仅仅是导航,还可能是在设置自定义对象之后进行导航。

    首先要将listbox选定项绑定到viewmodel中的属性

    <ListBox ItemsSource="{Binding Events}" Margin="0,0,-12,0" SelectedItem="{Binding SelectedEvent, Mode=TwoWay}">
    

    申报您的 SelectedEvent 财产

        public const string SelectedEventPropertyName = "SelectedEvent";
    
        private Event _selectedEvent;
    
    
        public Event SelectedEvent
        {
            get {return _selectedEvent;}
    
            set
            {
                if (_selectedEvent == value)
                {
                    return;
                }
    
                var oldValue = _selectedEvent;
                _selectedEvent = value;
    
                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(SelectedEventPropertyName, oldValue, value, true);
            }
        }
    

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <cmd:EventToCommand Command="{Binding EventPageCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    

    public RelayCommand EventPageCommand { get; private set; }
    public MainViewModel()
    {
        EventPageCommand = new RelayCommand(GoToEventPage);
    }
    

    最后宣布 GoToEventPage 方法

    private void GoToEventPage()
    {
        _navigationService.NavigateTo(new Uri("/EventPage.xaml", UriKind.Relative));
    }