我正在尝试创建一个列表视图,我将其绑定到类票证的OservableCollection。它可以很好地创建列表,但是当触发项目单击事件时,SelectedIndex始终为-1。如何获取所点击项目的索引?每个项目中都有一些对象。如何获得listviewitem中对象的地址能力。我可以从故障单对象中获取文本,但是访问该复选框,或者我可能需要放入的任何对象让我感到困惑。这是一个Windows Phone 8.1项目。
Xaml代码
<Page
x:Class="BindingTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BindingTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<ListView x:Name="LV" IsItemClickEnabled="True" ItemClick="LV_ItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid x:Name="LVIGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center"
MinWidth="0"/>
<TextBlock Grid.Column="1" Text="{Binding iflag}"
VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="36" />
<TextBlock Grid.Column="2" Text="{Binding kflag}"
VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="36"/>
<TextBlock Grid.Column="3" Text="{Binding qty}"
VerticalAlignment="Center" HorizontalAlignment="Right"
FontSize="36"/>
<TextBlock Grid.Column="4" Text="{Binding itemStr}"
VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="24"/>
<TextBlock Grid.Column="5" Text="{Binding priceStr}"
VerticalAlignment="Center" HorizontalAlignment="Right"
FontSize="24"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
背后的C#代码
namespace BindingTest
{
public sealed partial class MainPage : Page
{
public ObservableCollection<ticket> ticketCol;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
ticketCol = new ObservableCollection<ticket>()
{
new ticket(){iflag="I",kflag="k",qty="1",itemStr="Test Item",priceStr="9.99"},
new ticket(){iflag="I",kflag="k",qty="2",itemStr="Test Item",priceStr="9.99"},
new ticket(){iflag="I",kflag="k",qty="3",itemStr="Test Item",priceStr="9.99"},
new ticket(){iflag="I",kflag="k",qty="4",itemStr="Test Item",priceStr="9.99"}
};
LV.ItemsSource = ticketCol;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
public void createTable()
{
}
public class ticket
{
public string iflag { get; set; }
public string kflag { get; set; }
public string qty { get; set; }
public string itemStr { get; set; }
public string priceStr { get; set; }
}
private void LV_ItemClick(object sender, ItemClickEventArgs e)
{
string test = ((ticket)e.ClickedItem).itemStr;
int index = LV.SelectedIndex;
}
}
}
答案 0 :(得分:2)
当SelectedIndex
事件被触发时,ItemClick
可能还没有改变。或者,您可以尝试使用IndexOf()
方法从ObservableCollection
获取点击项索引,例如:
private void LV_ItemClick(object sender, ItemClickEventArgs e)
{
var clickedItem = (ticket)e.ClickedItem;
string test = clickedItem.itemStr;
var collection = (ObservableCollection<ticket>)LV.ItemsSource;
int index = collection.IndexOf(clickedItem);
}
对于第二个问题,我认为从DataTemplate
获取UI控件有一种干净的方法。 XAML最适合使用数据绑定,因此我们可以操作底层模型/视图模型,而不是直接从C#操作UI。
如果你真的需要这样做,也许这可以提供帮助(虽然适用于WPF):WPF - Retrieve Child DataTemplate Control From Custom ListBoxItem