C# 自定义两个列表框的样式,一个在另一个内
我有两个列表框,一个在另一个里面 我有这个xaml代码将listbox绑定到一个列表。我想知道是否有可能改变这一点: 为此: 是否可以禁用第二个列表框的属性?这是我的xaml代码:C# 自定义两个列表框的样式,一个在另一个内,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我有两个列表框,一个在另一个里面 我有这个xaml代码将listbox绑定到一个列表。我想知道是否有可能改变这一点: 为此: 是否可以禁用第二个列表框的属性?这是我的xaml代码: <ListBox Grid.Row="4" x:Name="MyList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8" HorizontalContentAlignment="Stretch" Alternatio
<ListBox Grid.Row="4" x:Name="MyList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8" HorizontalContentAlignment="Stretch" AlternationCount="2">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="cal:Message.Attach" Value="[Event MouseDoubleClick] = [Action OpenItem($dataContext)]"/>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#EFEFEF" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="100" >
<TextBlock Text="{Binding Name}" VerticalAlignment="Top" FontSize="16" FontWeight="Bold" Margin="8"/>
<TextBlock Margin="8" VerticalAlignment="Center"><Run Text="Date operation" /> - <Run Text="{Binding Data}" /></TextBlock>
<TextBlock Text="{Binding Total}" Margin="8" HorizontalAlignment="Right" VerticalAlignment="Stretch" />
<ListBox Height="auto" Background="Transparent" ItemsSource="{Binding SecondList}" Margin="8" IsEnabled="False" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Inxex}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
-
您需要在内部列表框中设置ItemContainerStyle
。在样式中,您需要将模板设置为:
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter/>
</ControlTemplate>
仅当您选择一个项目(改变背景颜色)时,此问题才出现吗?是,当我选择第一个列表框的一个项目时,第二个列表框的背景为灰色。我希望它是蓝色的,就像它是一个单一的列表框。我明白了。你能告诉我要修改的代码的全部部分吗?感谢您为内部项目添加了完整样式。
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="Bd"
Property="Background"
Value="LightBlue" />
</Trigger>
<Trigger Property="IsSelected"
Value="True">
<Setter TargetName="Bd"
Property="Background"
Value="Blue" />
<Setter Property="Foreground"
Value="White" />
</Trigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Foreground"
Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>