我有一个带有两个ListBox的WPF窗口。两个项目在运行时从视图模型添加,ListBox2(SelectionMode =“Multiple”)的项目依赖于ListBox1(SelectionMode =“Single”)选定项目。
现在,每当用户选择/取消选择ListBox2中的项目时,我想更新ListBox1的所选项目的背景。如果列表框2的所选项目数> 1,则ListBox1的selectedItem的背景应该更新。 0
此外,我希望无论选择哪个项目都保留背景,即假设我在ListBox1中的Obj 1并且我在ListBox2中选择了一些项目,现在即使我在ListBox1中选择Obj 3,Obj1的背景也不应该更改。我已经有一个哈希集来存储ListBox2的所有选定项目,后面的代码将在ListBox2的选择更改事件中添加/删除项目。
是否可以从后面的代码或使用样式触发器执行此操作?
以下是使事情更清晰的代码
<Border Grid.Column="1">
<Grid>
<ListBox x:Name="ListBox1" Style="{StaticResource ListBoxStyle}" SelectionMode="Single"
SelectionChanged="ListBox1_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="ObjBorder" Height="45">
<Grid x:Name="lstItemGrid" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.8*"/>
<ColumnDefinition Width="0.2*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Height="45">
<TextBlock x:Name="Text" Text="{Binding LB1Name}" />
</Border>
<Path Grid.Column="1" x:Name="CheckMark" Style="{StaticResource CheckStyle}">
</Path>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsVisible, ElementName=CheckMark}" Value="Visible">
<Setter Property="Background" TargetName="lstItemGrid" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
<Border Grid.Column="2" BorderThickness="0" VerticalAlignment="Stretch">
<Grid>
<ListBox x:Name="ListBox2" Style="{StaticResource ListBoxStyle}" SelectionMode="Multiple" SelectionChanged="ListBox2_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="ObjBorder" BorderBrush="Transparent" Height="45">
<Grid x:Name="lstItemGrid" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.8*"/>
<ColumnDefinition Width="0.2*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Height="45">
<TextBlock x:Name="Text" Text="{Binding LB2Name}" />
</Border>
<Path Grid.Column="1" x:Name="CheckMark" Style="{StaticResource CheckStyle}"></Path>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter Property="Background" TargetName="lstItemGrid" Value="Red"/>
<Setter Property="Visibility" TargetName="CheckMark" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>