【问题标题】:Mutually exclusive selection of two listviews两个列表视图的互斥选择
【发布时间】:2016-05-23 09:57:50
【问题描述】:

我已经实现了一个类似于the one made by Diederik Krols 的 UWP SplitView。我更喜欢使用 ListView 而不是使用 RadioButtons 的方法,如Jerry Nixon's implementation of the SplitView 所示。

但是,当我在 SplitView 底部添加辅助命令时,我遇到了一个问题,Diederik 没有这样做。这些辅助命令由绑定到命令集合的另一个 ListView 实现。所以我有两个 ListViews 应该一次只允许在其中选择一个项目。

我尝试了两件事:

  1. 我已将两个 ListView 的 SelectedItem 属性绑定到同一个对象。这个想法是,如果 SelectedItem 不在绑定到 ItemsSource 的列表中,ListView 可能不会显示选择。遗憾的是,它只是继续显示最后选择的项目。
  2. 我已将每个 ListView 的 SelectedItem 绑定到其自己的属性。 When one of the ListViews' item is selected, the SelectedItem of the other property is set to 'null' by the ViewModel.这会产生与 1 中相同的结果。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: xaml windows-store-apps win-universal-app


    【解决方案1】:

    我遇到了同样的问题。 我有一个解决方案,但我并不为此感到自豪;)这是一个肮脏的黑客,我希望其他解决方案能够出现,这样我也可以改变它。

    但这里是:

    首先列表视图连接到 SelectionChanged 事件,即使我们还将所选项目绑定到视图模型(此处显示完整代码 https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/Controls/SidePaneControl.xaml

    <ListView x:Name="TopMenu"
              SelectionChanged="OnTopMenuSelectionChanged"
              Background="Transparent"
              ItemsSource="{x:Bind ViewModel.TopMenuItems}"
              ItemTemplateSelector="{StaticResource MenuItemTemplateSelector}"                 
              ItemContainerStyle="{StaticResource MenuItemContainerStyle}"
              SelectedItem="{x:Bind ViewModel.SelectedTopMenuItem, Mode=TwoWay, Converter={StaticResource XBindItemCastingConverter}}"
              Grid.Row="0" />
    

    在那个 SelectionChanged 中,我们将取消选择“其他”列表视图选择! (此处显示完整代码https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/Controls/SidePaneControl.xaml.cs) 请注意,我们需要跟踪我们已经处于取消选择过程中,否则我们将最终陷入无限循环。这是通过 _listViewChanging 字段完成的。

    private void OnBottomMenuSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!_listViewChanging)
        {
            _listViewChanging = true;
            TopMenu.SelectedIndex = -1;
            _listViewChanging = false;
        }
    }
    

    最后要做的是确保我们处理选择并在视图模型中再次清除它以进行下一次迭代(此处显示完整代码https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/ViewModels/SidePaneViewModel.cs

    public MenuItem SelectedBottomMenuItem
    {
        get { return _selectedBottomMenuItem; }
        set
        {
            if (Set(() => SelectedBottomMenuItem, ref _selectedBottomMenuItem, value))
            {
                if (value != null)
                {
                    if (string.IsNullOrEmpty(SelectedBottomMenuItem.Title))
                        HamburgerCommand.Execute(null);
    
                    if (SelectedBottomMenuItem.Title.Equals("settings", StringComparison.OrdinalIgnoreCase))
                        SettingsCommand.Execute(null);
    
                    SelectedBottomMenuItem = null;
                }
            }                
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 2021-12-07
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2022-11-19
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多