簡體   English   中英

在WPF中,如何增加另一個ListBox內的ListBox的索引?

[英]In WPF How can I increment the index of a ListBox inside of another ListBox?

我希望能夠訪問的索引ListBox是另一個內部ListBox ,並增加該索引。 我嘗試使用ItemContainerGenerator但是當我將Item投射為ListBoxItemsControl它返回null

我想增加后面的代碼或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;



        }

我建議您使用一個斷點並瀏覽可視化器(如果要使用變量,則使用小放大鏡圖標),以便您可以了解此代碼的工作原理。

將其放入您的按鈕事件處理程序中:

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 您也可以使用Binding to SelectedIndex屬性,但隨后必須確保已實現INotifyPropertyChanged

在此處輸入圖片說明

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。如果您不需要該功能,則可以將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM