如何使用 wpf 中的堆栈面板从 Listview 获取值

How to get value from Listview with a stackpanel in wpf

我有一个列表视图,它是使用 wpf 中的堆栈面板填充的。单击星值文本块时,我想获得每行的 pooja_name。

<ListView x:Name="bookedlist" HorizontalAlignment="Left" Height="449" Margin="679,238,0,0" VerticalAlignment="Top" BorderBrush="#00828790" Background="Transparent" Focusable="False">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel x:Name="stackkk" Orientation="Horizontal" >
                    <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
                        <TextBlock Text="{Binding Pooja_name}" TextAlignment="Left" Margin="5" Width="250"/>
                    </Border>
                    <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
                        <TextBlock Text="{Binding Name}" TextAlignment="Left" Margin="5" Width="250"/>
                    </Border>
                    <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
                        <TextBlock Text="{Binding Star}" TextAlignment="Left" MouseLeftButtonDown="Star_function" Margin="5" Width="95"/>
                    </Border>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

使用模型类填充列表视图

public class Booked
    {
        public string Pooja_name { get; set; }
        public string Name { get; set; }
        public string Star { get; set; }
    }

和一个 jsonarray

JArray bookedpoojalist = JArray.Parse(bookedval);
                List<Booked> booked = JsonConvert.DeserializeObject<List<Booked>>(bookedpoojalist.ToString());
                bookedlist.ItemsSource = booked;

我想在激活星号文本块 MouseLeftButtonDown 时获取 pooja 名称。

sender 参数的 DataContext 转换为 Booked:

private void Star_function(object sender, MouseButtonEventArgs e)
{
    TextBlock textBlock = (TextBlock)sender;
    Booked booked = textBlock.DataContext as Booked;
    if (booked != null)
    {
        string s = booked.Pooja_name;
        //...
    }
}