如何从WPF c#中的列表框从文本框获取所有值?

时间:2020-03-19 16:22:21

标签: c# wpf

我正在尝试从ListBox中获取所有值,该ListBox是我从类中绑定数据的。目前这还行不通,它返回的是我的类“ Sprints”的路径,而不是数据。

这是我的XAML

$JRE_HOME/lib/tzdb.dat

这是我的XAML后端Click方法

<form [formGroup] = "">

已存储API数据的类

<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>

也是存储API数据的类

        private void SelectionSprint_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

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

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

1 个答案:

答案 0 :(得分:-1)

第一个问题是调用MessageBox时显示的路径是因为您在类上缺少ToString()。如果要在MessageBox或控制台中打印或显示值,则需要覆盖Sprints类的ToString()方法。

示例:

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}";
    }
}