我有一个嵌套的列表视图,如下所示。它在一个按钮中显示学生姓名及其对应的标记。当我点击按钮时,我想获得简洁的行索引。我怎么才能得到它?
INT
<ListView x:Name="Mainlist" HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:ClassRoomList">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" ></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListView x:Name="sublist1" ItemsSource="{x:Bind CurrentStudentsArray}" Grid.Column="0" BorderBrush="Black" BorderThickness="0,0,0,1" HorizontalAlignment="Stretch" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:CurrentStudentsList">
<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 StudentName}"/>
<ListView x:Name="sublist2" ItemsSource="{x:Bind StudentsMarksArray}" Grid.Column="1" HorizontalAlignment="Stretch" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:StudentsMarksList">
<Button Width="120" Height="40" Text="{x:Bind MarkInEnglish}" Click="ShowDetails_btn_Click">
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Listview如下所示,
private void ShowDetails_btn_Click(object sender, RoutedEventArgs e)
{
int selectedIndx = Mainlist.SelectedIndex; //always return -1
}
我使用的Class如下所示,它的值绑定到一个可观察的集合。并将此集合设置为listview的itemsource
DIANA 35
DHAYA 45
HAYAN 55
}
namespace TestClass
{
public class ClassRoomList
{
public int ClassID{ get; set; }
public ObservableCollection<CurrentStudentsList> CurrentStudentsArray { get; set; }
}
public class CurrentStudentsList
{
public string StudentName { get; set; }
public ObservableCollection<StudentsMarksList> StudentsMarksArray { get; set; }
}
public class StudentsMarksList
{
public string MarkInEnglish { get; set; }
public string MarkInMaths{ get; set; }
}
答案 0 :(得分:1)
您可以使用def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('bookmark',
sa.Column('date_created', sa.DateTime(), nullable=True),
sa.Column('date_modified', sa.DateTime(), nullable=True),
sa.Column('bookmark_id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=255), nullable=True),
sa.Column('url', sa.String(length=255), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('bookmark_id')
)
类获得对父ListView
和父ListViewItem
的引用。然后,您可以使用VisualTreeHelper
的{{1}}方法来获取索引:
IndexOf
答案 1 :(得分:0)
发送方将包含引发事件的行项目的数据上下文。您可以在Click处理程序中对其进行处理:
private void ShowDetails_btn_Click(object sender, RoutedEventArgs e)
{
StudentMark selectedItem= ((FrameworkElement)sender).DataContext as StudentMark ;
//If you are specially interested in index you could get it by:
int index= CurrentStudentsList.StudentsMarksArray.IndexOf(selectedItem);
}
`