从嵌套列表视图获取单击的行的索引-UWP

时间:2018-07-11 09:54:50

标签: c# listview uwp

我有一个嵌套的listview。当单击它的按钮时,我要获取它的行索引。现在,我的索引始终为-1。

   <ListView x:Name="Mainlist" HorizontalAlignment="Stretch">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:CurrentBooksList">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" ></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <ListView x:Name="sublist1" ItemsSource="{x:Bind CurrentFoldersArray}" Grid.Column="0" BorderBrush="Black" BorderThickness="0,0,0,1" HorizontalAlignment="Stretch" >
                            <ListView.ItemTemplate>
                                <DataTemplate x:DataType="local:CurrentFoldersList">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                    <TextBlock Foreground="Black" FontWeight="Bold" Grid.Column="0" TextWrapping="Wrap"  HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind BookCode}"/>
                                        <ListView x:Name="sublist2" ItemsSource="{x:Bind CurrentBookArray1}" Grid.Column="1" HorizontalAlignment="Stretch" >
                                            <ListView.ItemTemplate>
                                                <DataTemplate x:DataType="local:CurrentBusList1">
                                                    <Button Width="120" Height="40" Text="{x:Bind Lockbook}"  Click="LockMyBook_btn_Click">
                                                    </Button>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>
                                        </ListView>
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

 private void LockMyBook_btn_Click(object sender, RoutedEventArgs e)
    {
        int selectedIndx = Mainlist.SelectedIndex; //always return -1
    }

当用户单击内部列表视图的按钮时,如何获取主列表视图的行号?

1 个答案:

答案 0 :(得分:0)

  

是的,它运行良好。通过获取主列表的行号,我想访问CurrentBooksList中的相应项目

嗯,很奇怪。如果您使用的是基本的UWP按钮控件,则该控件不具有Text属性,因此您的代码不起作用。但我不在乎这一点,您的问题与此无关。

在按钮的单击事件处理程序中,可以获取其DataContext,然后可以将其转换为CurrentBusList1对象。通过此CurrentBusList1对象,您可以从“ Mainlist” ItemsSource进行一些判断,并获取所选的CurrentBooksList对象。

public sealed partial class MainPage : Page
{
    public ObservableCollection<CurrentBooksList> currentBooksLists { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        currentBooksLists = new ObservableCollection<CurrentBooksList>();

        ObservableCollection<CurrentFoldersList> currentFoldersLists = new ObservableCollection<CurrentFoldersList>();
        currentFoldersLists.Add(new CurrentFoldersList() { BookCode = "abc123", CurrentBookArray1 = new ObservableCollection<CurrentBusList1>() { new CurrentBusList1() { Lockbook = "123abc" }, new CurrentBusList1() { Lockbook = "123def" } } });
        currentFoldersLists.Add(new CurrentFoldersList() { BookCode = "def456", CurrentBookArray1 = new ObservableCollection<CurrentBusList1>() { new CurrentBusList1() { Lockbook = "def456" } } });


        currentBooksLists.Add(new CurrentBooksList() { CurrentFoldersArray = currentFoldersLists });
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int index;
        var subSelectedItem = ((FrameworkElement)sender).DataContext as CurrentBusList1;

        foreach (var item in currentBooksLists)
        {
            foreach (var folder in item.CurrentFoldersArray)
            {
                foreach (var bus in folder.CurrentBookArray1)
                {
                    if (bus == subSelectedItem)
                    {
                        index = currentBooksLists.IndexOf(item);
                        break;
                    }
                }
            }
        }
    }
}

public class CurrentBooksList
{
    public ObservableCollection<CurrentFoldersList> CurrentFoldersArray { get; set; }

}

public class CurrentFoldersList
{
    public string BookCode { get; set; }
    public ObservableCollection<CurrentBusList1> CurrentBookArray1 { get; set; }
}

public class CurrentBusList1
{
    public string Lockbook { get; set; }
}
<ListView x:Name="Mainlist" HorizontalAlignment="Stretch" ItemsSource="{x:Bind currentBooksLists}">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:CurrentBooksList">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" ></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <ListView x:Name="sublist1" ItemsSource="{x:Bind CurrentFoldersArray}" Grid.Column="0" BorderBrush="Black" BorderThickness="0,0,0,1" HorizontalAlignment="Stretch" >
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="local:CurrentFoldersList">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Foreground="Black" FontWeight="Bold" Grid.Column="0" TextWrapping="Wrap"  HorizontalAlignment="Left" VerticalAlignment="Center" Text="{x:Bind BookCode}"/>
                                    <ListView x:Name="sublist2" ItemsSource="{x:Bind CurrentBookArray1}" Grid.Column="1" HorizontalAlignment="Stretch" >
                                        <ListView.ItemTemplate>
                                            <DataTemplate x:DataType="local:CurrentBusList1">
                                                <Button Width="120" Height="40" Content="{x:Bind Lockbook}" Click="Button_Click">
                                                </Button>
                                            </DataTemplate>
                                        </ListView.ItemTemplate>
                                    </ListView>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>