从点击的堆栈面板元素中检索 object

Retrieve object from tapped stackpanel element

如标题所示,在简单的 UWP 应用程序中点击堆栈面板列表中的元素时,我遇到了困难。堆栈面板将其项目源连接到 "Customers" 列表,然后我将显示

            ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
            // Create a new ListView (or GridView) for the UI, add content by setting ItemsSource
            ListView customersLV = new ListView();
            customersLV.ItemsSource = customers;

            // Add the ListView to a parent container in the visual tree (that you created in the corresponding XAML file)
            customerPanel.Children.Add(customersLV);

XAML-code 看起来像这样(为更长的列表添加了滚动查看器):

        <ScrollViewer VerticalScrollBarVisibility="auto" HorizontalScrollBarVisibility="auto" Margin="63,341,1043,368" >
            <StackPanel x:Name="customerPanel" Height="441" Width="394" DoubleTapped="customerPanel_DoubleTapped" ></StackPanel>
        </ScrollViewer>

虽然在列表中添加和删除项目效果很好,但双击列表面板时我似乎无法访问任何特定的 Customer-object。 这是我的双击事件函数:

    private void customerPanel_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
    {
        testText.Text = e.OriginalSource.ToString();
    }

这似乎只打印了对整个堆栈面板的引用,而不是对我 double-tapped 的特定 object 的引用。例如,如果我想调用它的 ToString-method?

,我该如何访问被窃听的 Customer-object

感谢您的宝贵时间:)

看来你只是想在双击ListViewItem时获取项目值,所以你只需要写一个ListView,而不是使用ScrollViewerStackPanel,等等。 所以我们可以这样写 xaml 代码:

<Grid>
        <ListView x:Name="listView" DoubleTapped="listView_DoubleTapped">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Customer">
                    <StackPanel Margin="0 10" Orientation="Horizontal">
                        <Image Width="50" Height="50" Source="{x:Bind Head, Mode=OneWay}"/>
                        <TextBlock Margin="30 0 0 0" FontSize="25" Text="{x:Bind Name, Mode=OneWay}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

然后我们定义一个Customerclass.

public class Customer
{
    public string Head { get; set; }
    public string Name { get; set; }
}

好的,我们已经完成了一半。接下来我们要创建一个数据集合,并把它交给ListViewItemSource.

public MainPage()
{
    this.InitializeComponent();

    this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    ObservableCollection<Customer> list = new ObservableCollection<Customer>();
    for (int i = 0; i < 100; i++)
    {
        Customer p = new Customer() 
        { 
            Head = "https://docs.microsoft.com/zh-cn/visualstudio/releases/2019/media/2019_rc_logo.png",
            Name = i.ToString() 
        };
        list.Add(p);
    }

    listView.ItemsSource = list;        
}

最后一步是完成 DoubleTapped 事件。

private void listView_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    var customer = listView.SelectedItem as Customer;
    Debug.WriteLine(customer.Name);
}

完成!!!

我想现在已经解决了! 我只是像这样在我的 StackPanel 中放置一个 listView。

<ScrollViewer VerticalScrollBarVisibility="auto" HorizontalScrollBarVisibility="auto" Margin="63,341,1043,368" >
            <StackPanel x:Name="customerPanel" Height="441" Width="394">
                <ListView x:Name="customersLV" Tapped="listView_Tapped">
                </ListView>
            </StackPanel>
</ScrollViewer>

CS 代码如下所示:

ObservableCollection<Customer> customers;
public MainPage()
{
      this.InitializeComponent();

      customers = new ObservableCollection<Customer>();

      customersLV.ItemsSource = customers;
}


private void listView_Tapped(object sender, TappedRoutedEventArgs e)
{
      testBox.Text = customersLV.SelectedItem.ToString();
}

这将在我的 testBox 文本块中打印出实际客户对象的字符串表示:)