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

时间:2013-12-17 11:09:38

标签: c# wpf xaml listbox

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

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

enter image description here

到此:

enter image description here

是否可以禁用第二个列表框的属性?这是我的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>

1 个答案:

答案 0 :(得分:2)

您需要在内部列表框中设置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>