简体   繁体   English

如何从 WPF c# 中的 ListBox 中的 TextBox 获取所有值?

[英]How to get all values from a TextBox from a ListBox in WPF c#?

I'm trying to grab all the values from a ListBox which I binded data from my class.我试图从我从我的班级绑定数据的 ListBox 中获取所有值。 At the moment this hasn't worked and it returns the path of my class "Sprints" but not the data.目前这还没有奏效,它返回我的类“Sprints”的路径而不是数据。

This is my XAML这是我的 XAML

<UserControl x:Class="Prototype.Sprint_Planning.PlanningIndex"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Prototype.Sprint_Planning"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             mc:Ignorable="d"
             d:DesignHeight="570" d:DesignWidth="830">

    <!-- whole projects grid -->
    <Grid Grid.Column="1" Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="60*" />
            <RowDefinition Height="239*" />

        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>

        </Grid.ColumnDefinitions>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA0A0A0" Offset="1" />
                <GradientStop Color="#FFDADADA" />
            </LinearGradientBrush>
        </Grid.Background>

        <!-- create new project button -->
        <Grid Column="2">
            <Button Margin="10">
                <Grid Width="230">
                    <materialDesign:PackIcon Kind="Plus" />
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Maak nieuwe Sprint aan"
                               FontFamily="Century Gothic" />
                </Grid>
            </Button>
        </Grid>


        <!-- project buttons -->
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="98*"/>
                <RowDefinition Height="17*"/>
            </Grid.RowDefinitions>
            <!-- LIST -->
            <ListView Margin="10,27,10,-444" Name="lvDataBinding" Grid.Row="1">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <TextBlock x:Name="SelectionSprint" Text="Name: " />
                            <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                            <TextBlock Text=", " />
                            <TextBlock MouseLeftButtonUp="SelectionSprint_MouseLeftButtonUp" x:Name="Id" Text="{Binding Id}" Tag="{Binding Id}" FontWeight="Bold" />
                            <TextBlock Text=" (" />
                            <TextBlock Text="{Binding Start_Date}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                            <TextBlock Text=")" />
                        </WrapPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Grid>
</UserControl>

This is my XAML back-end Click method这是我的 XAML 后端 Click 方法

        private void SelectionSprint_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            var item1 = ((ListBoxItem)lvDataBinding.SelectedValue).Content.ToString();

            MessageBox.Show(item1.ToString());
        }

Class where API data has been stored存储 API 数据的类

    public class SprintList
    {
        public List<Sprints> Sprints { get; set; }
    }

Also a Class where API data has been stored也是一个存储 API 数据的类

    public class Sprints
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Slug { get; set; }
        public int Setup { get; set; }
        public string Start_Date { get; set; }
        public string End_Date { get; set; }
        public int ProjectId { get; set; }
        public string Created_At { get; set; }
        public string Updated_At { get; set; }
    }

First problem is your path is showing when you call the MessageBox is because you are missing a ToString() on your class.第一个问题是您在调用 MessageBox 时显示的路径是因为您在类中缺少 ToString() 。 If you like to print or show the values in a MessageBox or console then you need to overwrite the ToString() method of your Sprints class.如果您想在 MessageBox 或控制台中打印或显示值,则需要覆盖 Sprints 类的 ToString() 方法。

Example:例子:

public class Sprints
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Slug { get; set; }
    public int Setup { get; set; }
    public string Start_Date { get; set; }
    public string End_Date { get; set; }
    public int ProjectId { get; set; }
    public string Created_At { get; set; }
    public string Updated_At { get; set; }

    public override string ToString()
    {
       return $"{Name}";
    }
}

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

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