【问题标题】:Synchronize (multi)selection between two listboxes在两个列表框之间同步(多)选择
【发布时间】:2016-01-07 11:37:06
【问题描述】:

我有两个绑定到同一个数据源(文件名数组)的列表框。一个列表框将文件视为图标,而另一个列表框将它们视为列表。现在我希望他们有相同的选择。因此,当我在一个文件中多选两个或多个文件时,也应该在另一个文件中选择相同的文件。有没有办法做到这一点,最好在 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>

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    试试这个代码

    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");
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-27
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多