在两个列表框之间同步(多个)选择

时间:2016-01-07 11:37:07

标签: c# wpf listbox

我有两个绑定到同一数据源的列表框(文件名数组)。一个列表框将文件视为图标,而另一个列表框将它们视为列表。现在我希望他们有相同的选择。因此,当我在一个文件中多选两个或多个文件时,也应在另一个文件中选择相同的文件。有没有办法做到这一点,最好是在XAML ?我尝试IsSynchronizedWithCurrentItem并绑定到SelectedItem,但这些只适用于单个选择。

<Listbox Name="listView" ItemsSource="{Binding Files}">
    <Listbox.ItemTemplate>
        // View as list
    </Listbox.ItemTemplate>
</ListBox>

<Listbox Name="IconView" ItemsSource="{Binding Files}">
    <Listbox.ItemTemplate>
        // View as Icon grid
    </Listbox.ItemTemplate>
</ListBox>

1 个答案:

答案 0 :(得分:0)

试试此代码

XAML

 <StackPanel>
        <ListBox Height="100" SelectionMode="Multiple" SelectedItem="{Binding SelectedListItem}" x:Name="listview"  ItemsSource="{Binding ListBoxItems}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Item}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
        <ListBox Height="100" x:Name="itemview" SelectionMode="Multiple"  SelectedItem="{Binding SelectedListItem}" ItemsSource="{Binding ListBoxItems}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Item}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </StackPanel>

视图模型

    private ObservableCollection<MultipleSelection> listBoxItems = new ObservableCollection<MultipleSelection>();
            public ObservableCollection<MultipleSelection> ListBoxItems
            {
                get
                {
                    return listBoxItems;
                }
                set
                {
                    listBoxItems = value;
                    this.RaisePropertyChanged("ListBoxItems");
                }
            }

            private MultipleSelection selectedListItem;
            public MultipleSelection SelectedListItem
            {
                get
                {
                    return selectedListItem;
                }
                set
                {
              selectedListItem = value;
            var selectedItems = ListBoxItems.Where(x => x.IsSelected);
            this.RaisePropertyChanged("SelectedListItem");

                }
            }

public class MultipleSelection : INotifyPropertyChanged
        {
            private string item;

            public string Item
            {
                get { return item; }
                set
                {
                    item = value;
                    this.RaisePropertyChanged("Item");
                }
            }

            private bool isSelected;

            public bool IsSelected
            {
                get { return isSelected; }
                set
                {
                    isSelected = value;
                    this.RaisePropertyChanged("IsSelected");
                }
            }
        }