簡體   English   中英

如何在WPF中使用StackPanel從Listview獲取值

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

我有一個使用wpf中的stackpanel填充的listview。 單擊星號值文本塊時,我想獲取每一行的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

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激活時,我想獲取字母名稱。

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