2014-11-14 93 views
1

我希望能夠訪問另一個ListBox中的ListBox的索引並增加該索引。我試圖使用ItemContainerGenerator,但當我將該物品投放爲ListBoxItemsControl時,它會返回null在WPF中如何增加另一個ListBox內的列表框的索引?

我想增加代碼後面的索引或viewmodel。

這裏是我的模板的基本結構

<Window x:Class="WpfApplication12.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <Style x:Key="MyListStyle" TargetType="{x:Type ListBox}"> 

      <Setter Property="BorderThickness" Value="0"></Setter> 
      <Setter Property="SelectedIndex" Value="0"></Setter> 

      <Setter Property="ItemsPanel"> 
       <Setter.Value> 
        <ItemsPanelTemplate> 
         <VirtualizingStackPanel Orientation="Horizontal"> 

         </VirtualizingStackPanel> 
        </ItemsPanelTemplate > 
       </Setter.Value> 
      </Setter> 
      <Setter Property="ItemContainerStyle"> 
       <Setter.Value> 
        <Style TargetType="{x:Type ListBoxItem}" > 
         <Setter Property="Visibility" Value="Collapsed"></Setter> 

         <!--<Setter Property="Margin" Value="2" />--> 
         <Setter Property="Template"> 

          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
            <ListBox Name="InnerList" ItemsSource="{Binding}" ></ListBox> 
           </ControlTemplate> 
          </Setter.Value> 

         </Setter> 
         <Style.Triggers> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter Property="Visibility" Value="Visible"/> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition></RowDefinition> 
      <RowDefinition></RowDefinition> 
     </Grid.RowDefinitions> 

     <Button Grid.Row="1" Click="Button_Click">button</Button> 
     <ListBox Style="{StaticResource MyListStyle}" Name="ListItemsControl" VirtualizingPanel.IsVirtualizing="True" Grid.Row="0"></ListBox> 
    </Grid> 
</Window> 

Here is some code to load the list 


     public MainWindow() 
     { 
      InitializeComponent(); 

      CompositeCollection cc = new CompositeCollection(); 
      cc.Add(new List<int>() { 1, 2, 3, 4, 5 }); 
      cc.Add(new List<int>() { 6, 7, 8, 9, 10 }); 
      cc.Add(new List<int>() { 11, 12, 13, 14, 15 }); 
      ListItemsControl.ItemsSource = cc; 



     } 
+0

那麼,內部的'ListBox'實際上是在另一個'ListBox'裏面呢,還是隻是想根據另一個'ListBox'的選定索引增加一個'ListBox'? – 2014-11-14 18:58:16

+0

我有一個列表框填充列表框我希望能夠遞增內部列表框的索引時,該列表框已被選中 – Bob 2014-11-14 19:00:41

+0

您是否使用MVVM模式或代碼隱藏? – 2014-11-14 19:01:26

回答

1

我建議你使用一個斷點,(如果你想在一個變量的小放大鏡圖標),通過可視化走,這樣你可能會得到一個想法這個代碼如何工作。

將這個到您的按鈕事件處理程序:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    //var item = ListItemsControl.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem; 
    //var innerListBox = VisualTreeHelper.GetChild(item, 0) as ListBox; 
    //innerListBox.SelectedIndex++; 

    // For every item in the ListItemsControl 
    for (int i = 0; i < ListItemsControl.Items.Count; i++) 
    { 
     // Get the item container for the specified index and cast it as ListBoxItem. 
     var item = ListItemsControl.ItemContainerGenerator.ContainerFromIndex(i) 
      as ListBoxItem; 
     // Then, get the first child of the ListBoxItem and cast it as a ListBox. 
     // Note that I'm making an assumption that it'll always be a ListBox, 
     // which is why you should perform some checks in a production case, 
     // to avoid exceptions. 
     var innerListBox = VisualTreeHelper.GetChild(item, 0) as ListBox; 
     // Lastly, I increment the index of this ListBox. 
     innerListBox.SelectedIndex++; 
    } 
} 

註釋掉是隻改變一個元素的索引方式。在下面,我增加了所有三個內部列表框的索引。這給了你一個如何到達他們的想法,所以你可以根據自己的喜好改變它。顯然,您可能想要添加代碼來檢查null,並在嘗試增加SelectedIndex屬性之前確認正確的類型,但這並不困難。

老答案(基於第一篇):

這是一個代碼隱藏的例子。讓我知道如果你想要一個MVVM之一。您也可以使用BindingSelectedIndex屬性,但您必須確保您已執行INotifyPropertyChanged

enter image description here

XAML

<Window x:Class="LB.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="219.965" Width="217.535"> 
    <StackPanel> 
     <ListBox x:Name="lbOuter" HorizontalContentAlignment="Stretch"> 
      <ListBox.Items> 
       <TextBlock>Outer Item #1</TextBlock> 
       <TextBlock>Outer Item #1</TextBlock> 
       <ListBox x:Name="lbInner" BorderBrush="Black" BorderThickness="1" Margin="5"> 
        <ListBox.Items> 
         <TextBlock>Inner Item #1</TextBlock> 
         <TextBlock>Inner Item #2</TextBlock> 
         <TextBlock>Inner Item #3</TextBlock> 
        </ListBox.Items> 
       </ListBox> 
       <TextBlock>Outer Item #3</TextBlock> 
       <TextBlock>Outer Item #4</TextBlock> 
      </ListBox.Items> 
     </ListBox> 
     <StackPanel Orientation="Horizontal"> 
      <Button Content="Increment Outer" Margin="5" Click="Button_Click"/> 
      <Button Content="Increment Inner" Margin="5" Click="Button_Click_1"/> 
     </StackPanel> 
    </StackPanel> 
</Window> 

代碼隱藏

using System.Windows; 

namespace LB 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      if (lbOuter.SelectedIndex < (lbOuter.Items.Count - 1)) 
      { 
       lbOuter.SelectedIndex++; 
      } 
      else 
      { 
       lbOuter.SelectedIndex = 0; 
      } 
     } 

     private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      if (lbInner.SelectedIndex < (lbInner.Items.Count - 1)) 
      { 
       lbInner.SelectedIndex++; 
      } 
      else 
      { 
       lbInner.SelectedIndex = 0; 
      } 
     } 
    } 
} 

上面的代碼實際上將循環您的選擇。所以,如果你達到最後,它會讓你索引0.如果你不想要這個功能,你可以刪除它。

+1

我的安裝程序正在使用CompositeCollection的數據模板。而且我無法訪問ListBox的ItemsContainer的控件模板中設置的ListBox的名稱。 – Bob 2014-11-14 19:19:00

+0

@Bob啊,我沒有意識到這是基於你原來的帖子。 – 2014-11-14 19:48:23

+0

@BK我知道我很抱歉,我意識到我原來的帖子並不清楚,並更新我的描述。 – Bob 2014-11-14 19:51:31