2014-10-03 267 views
0

我有一個獨特的請求,我對WPF很新。 我有一個項目列表。當我選擇一個項目時,它應該顯示子項目。
有很多方法可以做到這一點:
1.您有子列表框項目向下滑動父列表框項目向下移動。將會有很多彈跳
2.爲了避免高度變化,我在考慮子列表框項目可以彈出,我選擇了子項目。如果沒有,那麼我點擊退回到我的父母列表框項目。
WPF列表框選定項目調出另一個子列表框項目

我不知道從哪裏開始添加一個子列表框項目下面的父項列表項。 我有兩個列表框在我的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> 
+0

如果我是你,我就你LBItems綁定到'的ObservableCollection ',然後使用'OC.Add'和'OC.Remove'更改的項目。 – Hosch250 2014-10-03 21:31:29

+0

歡迎來到SO。沒有100件事情在繼續。只需發佈足夠的問題即可。 – Paparazzi 2014-10-03 21:52:53

回答

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你可以有這樣的事情:

接着,當窗口或用戶控件資源你有:

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


</Grid> 
</DataTemplate> 

這是不完美的代碼...但希望它讓你的頭開始你想要什麼。

問候,

+0

你也可以註冊父母Listbox selectionChanged並打開一個新窗口... – sexta13 2014-10-03 22:42:04

相關問題