繁体   English   中英

自定义两个列表框的样式,一个在另一个内

[英]Customize the style of two listboxes one inside the other

我有两个列表框,一个在另一个里面。

我有这个xaml代码将列表框绑定到List。 我想知道是否可以改变这个:

在此输入图像描述

对此:

在此输入图像描述

是否可以禁用第二个列表框的属性? 这是我的xaml代码:

<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>

这是符合上述设计的裸骨模板。 如果您需要选择内部项目,则使用Border将ContentPresenter设置为透明背景,然后使用触发器控制背景。

编辑

这是内部项目的完整样式选择:

<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>

暂无
暂无

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

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