简体   繁体   English

ListBox 显示多个选中项

[英]ListBox display multiple selected items

I have here a ListBox which shall show serveral items where some of them are selected.我这里有一个ListBox ,它将显示选择其中一些项目的几个项目。

<ListBox IsEnabled="False" x:Name="SecondaryModelSelector" SelectionMode="Extended"  SelectedItem="{Binding Path=DataContext.SelectedSecondaryModels, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContentControl}, Mode=OneWay}" ItemsSource="{Binding Path=DataContext.AvailableModels, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContentControl}}" >
 <ListBox.ItemTemplate>
  <DataTemplate>
   <StackPanel Orientation="Horizontal">
    <Label Content="{Binding Name}" Margin="5,0,5,0"/>
   </StackPanel>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

The selected items are set in a ComboBox and thus the ListBox is disabled and Mode=OneWay to disable selection in the ListBox itself.所选项目在ComboBox中设置,因此ListBox被禁用, Mode=OneWay以禁用ListBox本身的选择。 The Template looks like this:模板如下所示:

<Style x:Key="MyListBoxItem" TargetType="ListBoxItem">
 <Setter Property="SnapsToDevicePixels"  Value="true" />
 <Setter Property="Template">
  <Setter.Value>
   <ControlTemplate TargetType="ListBoxItem">
    <Border x:Name="Border"  Padding="2">
     <ContentPresenter />
    </Border>
    <ControlTemplate.Triggers>
     <Trigger Property="IsSelected" Value="true">
      <Setter TargetName="Border" Property="Background" Value="Blue"/>
     </Trigger>
    </ControlTemplate.Triggers>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

<Style x:Key="MyListBoxBorder" TargetType="{x:Type ListBox}">
 <Setter Property="ItemContainerStyle" Value="{StaticResource MyListBoxItem}"/>
 <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="ListBox">
     <Border x:Name="OuterBorder">
      <ScrollViewer Margin="0"  Focusable="false">
       <StackPanel Margin="2" IsItemsHost="True" />
      </ScrollViewer>
    </Border>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

The problem is now, that I cannot display multiple selected items, ie the background does not turn blue.现在的问题是,我无法显示多个选定的项目,即背景不会变成蓝色。 Showing only one selected item is no problem.只显示一个选定的项目是没有问题的。

Works (but shows only one selected item...)工作(但只显示一个选定的项目......)

public Cpu SelectedSecondaryModels
{
    get  { return SelectedGlobalVariable.SecondaryModels.FirstOrDefault();  }
}

Does not work:不工作:

public ObservableCollection<Model> SelectedSecondaryModels
{
    get  { return new ObservableCollection<Model>(SelectedGlobalVariable.SecondaryModels);  }
}

I have no errors, so how can I show multiple selected items?我没有错误,那么如何显示多个选定的项目? I tried SelectedItems instead of SelectedItem instead, but this is read-only field.我尝试了SelectedItems而不是SelectedItem ,但这是只读字段。 When I allow to select items in the ListBox than the blue background appears, and binding throws errors as there is no setter method.当我允许ListBox中的 select 项目比蓝色背景出现时,由于没有设置方法,绑定会引发错误。

The SelectedItem property cannot be bound to a collection of several items to be selected. SelectedItem属性不能绑定到要选择的多个项目的集合。

And as you have already noticed, SelectedItems is a read-only property.正如您已经注意到的, SelectedItems是一个只读属性。

You could use an attached behaviour to work around this.您可以使用附加行为来解决此问题。 Please refer to this blog post and this example for more information about how to do this.有关如何执行此操作的更多信息,请参阅此博客文章此示例

You will also find some solutions here:您还可以在这里找到一些解决方案:

Bind to SelectedItems from DataGrid or ListBox in MVVM 从 MVVM 中的 DataGrid 或 ListBox 绑定到 SelectedItems

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM