多选ListView

Multiple selection ListView

我有两个 ListView 控件。在左侧我显示国家列表,右侧显示相应的城市列表。
现在问题来了:我想用选定城市的列表填充第三个 ListView。我已经想出了一种不多次添加城市的方法,但我仍然遇到为不再选择的国家/地区添加选定城市的问题。 例如:

当我按 "Add" 时,我只想添加当前所选国家/地区的城市,在本例中为柏林和慕尼黑。

到目前为止的部分视图:

<ListView ItemsSource="{Binding CountryList, Mode=TwoWay}"
           SelectionMode="Single"
           SelectedItem="{Binding SelectedCountry, Mode=TwoWay}">
               <ListView.ItemTemplate>
                     <DataTemplate>
                         <TextBlock Text="{Binding Name}"/>
                     </DataTemplate>
               </ListView.ItemTemplate>
</ListView>
<...>
<ListView ItemsSource="{Binding CityList, Mode=TwoWay}"
          SelectionMode="Extended">
          <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                      <Setter Property="IsSelected" Value="{Binding isSelected}"/>
                </Style>
          </ListView.ItemContainerStyle>
             <ListView.ItemTemplate>
                 <DataTemplate>
                     <TextBlock Text="{Binding Name}"/>
                 </DataTemplate>
             </ListView.ItemTemplate>
</ListView>
<ListView ItemsSource="{Binding AddedCities}">
     <ListView.ItemTemplate>
          <...>
     </ListView.ItemTemplate>
</ListView>

我的 ViewModel 实现了以下(以及更多)属性:

public Country SelectedCountry
        {
            get { return GetValue(() => SelectedCountry); }
            set
            {
                SetValue(() => SelectedCountry, value);

                var query = from c in CityList
                            where c.Country == SelectedCountry
                            select c;

                CitiesInCountry= new ObservableCollection<City>(query);
            }
        }

public IEnumerable<City> SelectedCities { get { return CityList.Where(x => x.IsSelected); } }

这可能不是解决问题的最佳方法,但我仍在学习。 如何以及何时重置所选城市?这可能吗?

我设法找到了解决方案。它适用于我的情况,有时最明显的答案是我们最后想到的。

public IEnumerable<City> SelectedCities{ get { return CityList.Where(x => x.IsSelected && x.Country == SelectedCountry); } }

这不会阻止同时为不同国家选择多个城市,但会抑制那些不属于所选国家的城市。