WPF列表框选定项目会显示另一个子列表框项目

时间:2014-10-03 21:17:06

标签: c# wpf listbox

我有一个独特的请求,我对WPF很新。 我有一个项目列表。当我选择一个项目时,它应该显示子项目 有很多方法可以做到这一点:
1.您将子列表框项向下滑动,将父列表框项向下移动。周围会有很多弹跳 2.为了避免高度变化,我认为子列表框项可以弹出,我选择子项。如果没有,那么我点击即可返回我的父列表框项目。

我不知道从哪里开始在其父列表框项下添加子列表框项。 我的xaml中有两个列表框

<Window x:Class="MakeModel.MakeModelYear"
    Icon="cc_64x64_blue_02.ico"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MakeModel" 
    Title="Car Make and Model" Height="740.667" Width="426" Opacity="0.9" 
    WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Background="White">
<Window.Resources>
</Window.Resources>
<Grid>
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFFFFE" Offset="0"/>
            <GradientStop Color="#FF6699CC" Offset="1.2"/>
        </LinearGradientBrush>
    </Grid.Background>
    <Grid.RowDefinitions>
        <RowDefinition Height="156*"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition Width="53*"/>
        <ColumnDefinition Width="198*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Margin="0,31,0,29" Grid.ColumnSpan="3">
        <GroupBox Header="Make / Model" BorderBrush="WhiteSmoke" BorderThickness="0" Margin="5,0" Foreground="#FF0B6C78" FontSize="18" FontFamily="Microsoft Sans Serif">
            <!--Task -->
            <StackPanel>
                <ListBox x:Name="cmbMake" HorizontalAlignment="Left" VerticalAlignment="Top" Width="387" Cursor="Arrow" 
                    ItemsSource="{Binding SelectedMake}" DisplayMemberPath ="Make" 
                    SelectionChanged="cmbMake_SelectionChanged" SelectionMode="Single" RenderTransformOrigin="0.494,1.409" Margin="0,3,0,0" Height="150" BorderBrush="#FF336699" FontFamily="Microsoft Sans Serif" FontSize="12" >
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Style.Resources>
                                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                            </Style.Resources>
                            <EventSetter Event="UIElement.MouseEnter" Handler="cmbFirstDigitLineItem_MouseMove" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True" >
                                    <Setter Property="Background" Value="#FFD7E1EC" />
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True" >
                                    <Setter Property="FontWeight" Value="Bold" />
                                    <Setter Property="FontSize" Value="24" />
                                    <Setter Property="Foreground" Value="Black" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.Template>
                        <ControlTemplate>
                            <Border CornerRadius="5" BorderThickness="0" BorderBrush="#FF336699">
                                <ItemsPresenter/>
                            </Border>
                        </ControlTemplate>
                    </ListBox.Template>
                </ListBox>
            </StackPanel>
        </GroupBox>
    </StackPanel>
    <StackPanel Margin="0,205,0,29" Grid.ColumnSpan="3">
        <GroupBox Header="" BorderBrush="WhiteSmoke" BorderThickness="0" Margin="5,0">
            <!--Subtask -->
            <StackPanel>
                <ListBox x:Name="cmbModel" HorizontalAlignment="Left" VerticalAlignment="Top" Width="387" Cursor="Arrow" 
                    ItemsSource="{Binding SelectedModel}" DisplayMemberPath ="Model" 
                    SelectionChanged="cmbModel_SelectionChanged" SelectionMode="Single" RenderTransformOrigin="0.494,1.409" Margin="0,3,0,0" Background="#FFE0E0E0" BorderBrush="#FF336699" FontFamily="Arial">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Style.Resources>
                                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                            </Style.Resources>
                            <EventSetter Event="UIElement.MouseEnter" Handler="cmbSecondDigitLineItem_MouseMove" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True" >
                                    <Setter Property="Background" Value="#FFD7E1EC" />
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True" >
                                    <Setter Property="FontWeight" Value="Bold" />
                                    <Setter Property="FontSize" Value="24" />
                                    <Setter Property="Background" Value="Transparent" />
                                    <Setter Property="Foreground" Value="Black" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.Template>
                        <ControlTemplate>
                            <Border CornerRadius="5" BorderThickness="0" BorderBrush="#FF336699">
                                <ItemsPresenter/>
                            </Border>
                        </ControlTemplate>
                    </ListBox.Template>
                </ListBox>
            </StackPanel>
        </GroupBox>
    </StackPanel>
</Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

好吧,让我试着用朴实简单的英语解释:)

首先,你的课程应该是这样的:

public class Foo{

public ObservableCollection<MyParentObject> ListToBind {get;set;}

.... (rest of properties)
}

public class MyParentObject{

public ObservableCollection<MyChildObject> ChildListToBind {get;set;}
.... (rest of properties)
}

然后在你的XAML中你可以得到这样的东西:

然后作为Window或UserControl资源:

<DataTemplate x:key="listItemTemplate">
<Grid>
<ListBox ItemsSource={Binding ChildListToBind}" ItemTemplate="........"/>


</Grid>
</DataTemplate>

这不是完美的代码...但希望它能让您领先于您想要的东西。

此致