Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ListViewItem获取对象并选择索引_C#_Xaml_Listview_Windows Phone 8 - Fatal编程技术网

C# 从ListViewItem获取对象并选择索引

C# 从ListViewItem获取对象并选择索引,c#,xaml,listview,windows-phone-8,C#,Xaml,Listview,Windows Phone 8,我正在尝试创建一个绑定到类票证的OservableCollection的列表视图。它很好地创建了列表,但当触发item click事件时,SelectedIndex始终为-1。如何获取已单击项的索引?每件物品里面都有一些物品。如何获得listviewitem中对象的地址功能。我可以从ticket对象中获取文本,但是访问复选框或任何我可能需要放入其中的对象会让我感到困惑。这是一个windows phone 8.1项目 Xaml代码 <Page x:Class="BindingTest.Mai

我正在尝试创建一个绑定到类票证的OservableCollection的列表视图。它很好地创建了列表,但当触发item click事件时,SelectedIndex始终为-1。如何获取已单击项的索引?每件物品里面都有一些物品。如何获得listviewitem中对象的地址功能。我可以从ticket对象中获取文本,但是访问复选框或任何我可能需要放入其中的对象会让我感到困惑。这是一个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;
    }
}
名称空间绑定测试
{
公共密封部分类主页面:第页
{
公众可观察到的收集票证;
公共主页()
{
this.InitializeComponent();
this.NavigationCacheMode=NavigationCacheMode.Required;
ticketCol=新的ObservableCollection()
{
新票证(){iflag=“I”,kflag=“k”,qty=“1”,itemStr=“测试项目”,priceStr=“9.99”},
新票证(){iflag=“I”,kflag=“k”,qty=“2”,itemStr=“测试项目”,priceStr=“9.99”},
新票证(){iflag=“I”,kflag=“k”,qty=“3”,itemStr=“测试项目”,priceStr=“9.99”},
新票证(){iflag=“I”,kflag=“k”,qty=“4”,itemStr=“测试项目”,priceStr=“9.99”}
};
LV.ItemsSource=ticketCol;
}
/// 
///当此页面即将显示在框架中时调用。
/// 
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//TODO:准备在此处显示的页面。
//TODO:如果应用程序包含多个页面,请确保
//通过注册来处理硬件后退按钮
//Windows.Phone.UI.Input.HardwareButtons.BackPressed事件。
//如果您正在使用某些模板提供的NavigationHelper,
//此事件已为您处理。
}
公共void createTable()
{
}
公务舱票
{
公共字符串iflag{get;set;}
公共字符串kflag{get;set;}
公共字符串数量{get;set;}
公共字符串itemStr{get;set;}
公共字符串priceStr{get;set;}
}
私有void LV_ItemClick(对象发送者,ItemClickEventArgs e)
{
字符串测试=((票证)e.ClickedItem.itemStr;
int index=LV.SelectedIndex;
}
}

}

可能
选择的索引
目前尚未更改
项单击
事件已触发。或者,您可以尝试使用方法从
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);
}
private void LV\u ItemClick(对象发送者,ItemClickEventArgs e)
{
var clickedItem=(票证)e.clickedItem;
字符串测试=clickedItem.itemStr;
var集合=(ObservableCollection)LV.ItemsSource;
int index=collection.IndexOf(单击编辑项);
}
对于第二个问题,我认为没有一种干净的方法可以从
DataTemplate
获取UI控件。XAML使用数据绑定最有效,因此我们可以操作底层模型/视图模型,而不是直接从C#操作UI


如果您真的需要这样做,也许这会有所帮助(不过这是针对WPF的):

谢谢您找到了索引,而链接正是我获取DataTemplate中对象所需要的。