我正在創建UWP項目。在這我使用listview來顯示內容。動態地將內容添加到列表視圖併成功完成它。我有一個問題,當我選擇的內容selectedIndex值總是-1。獲取listview選定的索引?
<ListView Name="MyListView" IsItemClickEnabled="False" SelectionMode="Single" SelectionChanged="MyListView_SelectionChanged_1">
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem Height="80" Width="290" Tapped="ListViewItem_Tapped" Margin="-10,0,-10,0">
<StackPanel Orientation="Horizontal" Margin="-10,0,-24,0" HorizontalAlignment="Right" Width="288" >
<Border Background="#E94F08" Height="75" Margin="0,0,0,0" Width="288" CornerRadius="50" >
<StackPanel Orientation="Horizontal">
<Ellipse Height="68" Margin="5,0,0,0" Width="70" HorizontalAlignment="Left" >
<Ellipse.Fill>
<ImageBrush ImageSource="/Assets/4.jpg"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock Margin="10,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Name }" Foreground="Black" FontSize="20">
</TextBlock>
</StackPanel>
</Border>
</StackPanel>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
只需注意:您不需要在數據模板中使用ListViewItem,當ListView創建項目時,整個數據模板將被ListViewItem包裝。 –
我也在數據模板中使用listviewitem。但這不是我的問題,我需要得到selectedindex或選定的價值,我不能得到那些請幫助。@ MehrzadChehraz –
提供的代碼是不夠的,找不到我的問題,至少不是。 –