ListBox 显示多个选中的项目
ListBox display multiple selected items
我这里有一个 ListBox
,它将显示几个项目,其中一些是 selected。
<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>
selected 项目在 ComboBox
中设置,因此 ListBox
被禁用,Mode=OneWay
禁用 selection 在 ListBox
本身。
模板如下所示:
<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>
现在的问题是,我无法显示多个 selected 项目,即背景没有变成蓝色。只显示一项 selected 项目没有问题。
有效(但仅显示一项 selected 项目...)
public Cpu SelectedSecondaryModels
{
get { return SelectedGlobalVariable.SecondaryModels.FirstOrDefault(); }
}
无效:
public ObservableCollection<Model> SelectedSecondaryModels
{
get { return new ObservableCollection<Model>(SelectedGlobalVariable.SecondaryModels); }
}
我没有错误,那么如何显示多个 selected 项目?
我尝试使用 SelectedItems
而不是 SelectedItem
,但这是只读字段。
当我允许 ListBox
中的 select 项目时,会出现蓝色背景,并且绑定会抛出错误,因为没有 setter 方法。
无法将SelectedItem
属性绑定到多个待选项目的集合。
正如您已经注意到的,SelectedItems
是只读的 属性。
您可以使用附加行为来解决此问题。有关如何执行此操作的更多信息,请参阅 this blog post and this example。
您还可以在这里找到一些解决方案:
Bind to SelectedItems from DataGrid or ListBox in MVVM
我这里有一个 ListBox
,它将显示几个项目,其中一些是 selected。
<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>
selected 项目在 ComboBox
中设置,因此 ListBox
被禁用,Mode=OneWay
禁用 selection 在 ListBox
本身。
模板如下所示:
<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>
现在的问题是,我无法显示多个 selected 项目,即背景没有变成蓝色。只显示一项 selected 项目没有问题。
有效(但仅显示一项 selected 项目...)
public Cpu SelectedSecondaryModels
{
get { return SelectedGlobalVariable.SecondaryModels.FirstOrDefault(); }
}
无效:
public ObservableCollection<Model> SelectedSecondaryModels
{
get { return new ObservableCollection<Model>(SelectedGlobalVariable.SecondaryModels); }
}
我没有错误,那么如何显示多个 selected 项目?
我尝试使用 SelectedItems
而不是 SelectedItem
,但这是只读字段。
当我允许 ListBox
中的 select 项目时,会出现蓝色背景,并且绑定会抛出错误,因为没有 setter 方法。
无法将SelectedItem
属性绑定到多个待选项目的集合。
正如您已经注意到的,SelectedItems
是只读的 属性。
您可以使用附加行为来解决此问题。有关如何执行此操作的更多信息,请参阅 this blog post and this example。
您还可以在这里找到一些解决方案:
Bind to SelectedItems from DataGrid or ListBox in MVVM