简体   繁体   English

如何在WPF中使用StackPanel从Listview获取值

[英]How to get value from Listview with a stackpanel in wpf

I have a listview which is populated using a stackpanel in wpf. 我有一个使用wpf中的stackpanel填充的listview。 I want to get pooja_name of each row when click on star value textblock. 单击星号值文本块时,我想获取每一行的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>

Listview is populated using a modelclass 使用模型类填充Listview

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

and a jsonarray 和一个jsonarray

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

I want to get pooja name when star value text block MouseLeftButtonDown is activated. 当星值文本块MouseLeftButtonDown激活时,我想获取字母名称。

Cast the DataContext of the sender argument to a Booked : 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;
        //...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM