• windows phone listbox的点击事件


    前台

    复制代码
    <ListBox x:Name="listbox1" Margin="6">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <Grid Margin="15" Tag="{Binding ImageID}" Tap="Post_Click">
    <StackPanel Orientation="Horizontal">
    <Image Source="{Binding Image}" Width="150" Height="120" Stretch="Fill"/>
    <TextBlock Text="{Binding ImageName}" FontSize="30" TextWrapping="Wrap" Width="300"/>
    </StackPanel>
    </Grid>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    复制代码

    后台

    复制代码
    private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0)
    return null;
    for (int i = 0; i < count; i++)
    {
    var child = VisualTreeHelper.GetChild(parentElement, i);
    if (child != null && child is T)
    {
    return (T)child;
    }
    else
    {
    var result = FindFirstElementInVisualTree<T>(child);
    if (result != null)
    return result;
    }
    }
    return null;
    }
    复制代码

    需要绑定

    复制代码
    private void Post_Click(object sender, System.Windows.Input.GestureEventArgs e)
    {
    var selectedIndex = listbox1.SelectedIndex;
    ListBoxItem item = listbox1.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as ListBoxItem;
    StackPanel border = FindFirstElementInVisualTree<StackPanel>(item);
    Image img = FindFirstElementInVisualTree<Image>(item);
    TextBlock txtBlock = FindFirstElementInVisualTree<TextBlock>(item);
    MessageBox.Show(txtBlock.Text.ToString());
    }
    复制代码
  • 相关阅读:
    一个datagrid中嵌入checkBox典型的例子
    堆排序算法
    堆排序(利用最大堆)
    动态代理模式的实现
    [转载]C#如何实现对外部程序的动态调用
    9.Jmeter 多个threadgroup 中的配置元件会一次性进行初始化
    二十七。
    三十。接口2
    三十三。日志
    大道至简读后感
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6947034.html
Copyright © 2020-2023  润新知