简体   繁体   English

在WPF中将文本框中的数据显示到ListView中

[英]Show data from textboxes into listview in wpf

I have a C# project have : 我有一个C#项目有:

The XAML Code: XAML代码:

<Window x:Class="Revision.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Patient Information" Height="456.36" Width="935.208">
<Window.Resources>
    <Style x:Key="SliderStyle">
        <Setter Property="FrameworkElement.Width" Value="100"/>
        <Setter Property="RangeBase.Minimum" Value="0"/>
        <Setter Property="RangeBase.Maximum" Value="100"/>
        <Setter Property="Slider.IsSnapToTickEnabled" Value="true"/>
        <Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
        <Setter Property="RangeBase.Value" Value="0"/>
        <Setter Property="Slider.AutoToolTipPlacement" Value="TopLeft"/>


    </Style>
</Window.Resources>
<Grid>
    <Label Content="First Name" Height="28" HorizontalAlignment="Left" Margin="19,23,0,0" Name="label1" VerticalAlignment="Top" />
    <Label Content="Last Name" Height="28" HorizontalAlignment="Left" Margin="19,82,0,0" Name="label2" VerticalAlignment="Top" />
    <Label Content="Address" Height="28" HorizontalAlignment="Left" Margin="20,144,0,0" Name="label3" VerticalAlignment="Top" />
    <Label Content="Security Type" Height="28" HorizontalAlignment="Left" Margin="19,203,0,0" Name="label4" VerticalAlignment="Top" />
    <TextBox Height="36" HorizontalAlignment="Left" Margin="105,23,0,0" Name="textBox1" VerticalAlignment="Top" Width="197" />
    <TextBox Height="36" HorizontalAlignment="Left" Margin="105,82,0,0" Name="textBox2" VerticalAlignment="Top" Width="197" />
    <TextBox Height="36" HorizontalAlignment="Left" Margin="105,136,0,0" Name="textBox3" VerticalAlignment="Top" Width="197" />
    <ComboBox Height="36" Margin="105,195,625,0" Name="comboBox1" VerticalAlignment="Top">
        <ComboBoxItem Content="Private Assurance" Name="PrA"/>
        <ComboBoxItem Content="Public Assurance" Name="PA"/>
        <ComboBoxItem Content="No Assurance" Name="NA"/>
    </ComboBox>
    <Button Content="Submit" Height="33" HorizontalAlignment="Left" Margin="147,365,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
    <Button Content="Display" Height="33" HorizontalAlignment="Left" Margin="227,365,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    <Label Content="Gender" HorizontalAlignment="Left" Margin="20,255,0,0" VerticalAlignment="Top"/>
    <RadioButton x:Name="maleRadio" Content="Male" HorizontalAlignment="Left" Margin="105,266,0,0" VerticalAlignment="Top"/>
    <RadioButton x:Name="femaleRadio" Content="Female" HorizontalAlignment="Left" Margin="192,266,0,0" VerticalAlignment="Top"/>
    <Slider x:Name="redSlider" Style="{StaticResource SliderStyle}" Value="{Binding Text, ElementName=textBox5}" Margin="74,313,636,56" SmallChange="1" Height="56" Width="Auto" />
    <TextBox x:Name="textBox5"  Text="{Binding Value, ElementName=redSlider}" Margin="296,313,588,86" SelectionOpacity="1" FontSize="13" />
    <Label Content="Age" HorizontalAlignment="Left" Margin="38,313,0,0" VerticalAlignment="Top"/>
    <ListView x:Name="ListView1" HorizontalAlignment="Left" Height="375" Margin="344,23,0,0" VerticalAlignment="Top" Width="567">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="First Name" Width="100"
DisplayMemberBinding="" />
                <GridViewColumn Header="Last Name" Width="80"
DisplayMemberBinding="" />
                <GridViewColumn Header="Address" Width="100"
DisplayMemberBinding="" />
                <GridViewColumn Header="Security Type" Width="80"
DisplayMemberBinding="" />
                <GridViewColumn Header="Gender" Width="100"
DisplayMemberBinding="" />
                <GridViewColumn Header="Age" Width="100"
DisplayMemberBinding="" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
</Window>

and I have a class patient: 我有一个班级病人:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revision
{
class Patient
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string Address { get; set; }
    public string securityType {get; set;}
    public string gender { get; set; }
    public string age { get; set; }

    public Patient(string fn, string ln, string ad, string st,string ge,string ag)
    {

        firstname = fn;
        lastname = ln;
        Address = ad;
        securityType = st;
        gender = ge;
        age = ag;

    }
    public override string ToString()
    {

        return string.Format("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5,-10}",

            firstname, lastname, Address, securityType, gender,age);
    }
}
}

and the main program 和主程序

public partial class MainWindow : Window
{
   // Patient [] patients = new Patient{}
    Patient [] patients = new Patient[100];
    private List<Patient> books = new List<Patient>();
    int i=0;
    string g;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        string x = "";
        if (PrA.IsSelected)
        {
            x = PrA.Content.ToString();
        }

        else if (PA.IsSelected)
        {
            x = PA.Content.ToString();
        }
        else if (NA.IsSelected)
        {
            x = NA.Content.ToString();
        }

        if (maleRadio.IsChecked == true)
            g = "Male";
        if(femaleRadio.IsChecked==true)
            g="Female";

       patients[i] = new Patient(
             textBox1.Text, textBox2.Text, textBox3.Text, g, textBox5.Text, x);
        i = i + 1;
       // Patient[] patients = { 
         //                        new Patient (
           //   textBox1.Text, textBox2.Text, textBox3.Text, x)};

        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();

        textBox5.Clear();


    }
}

So the question I want to display the data entered in the textboxes,radio button and combo box... in the List view 所以我想显示在列表视图的文本框,单选按钮和组合框中输入的数据的问题。

The normal way of getting your data into the listview would be through databinding. 将数据放入列表视图的通常方法是通过数据绑定。 Databinding is the way that WPF uses to transport data between your view and your code. 数据绑定是WPF在视图和代码之间传输数据的方式。

All wpf controls that can show multiple items, like listview, listbox, combobox etc, has a ItemSource property. 所有可以显示多个项目的wpf控件(如listview,listbox,combobox等)都具有ItemSource属性。 By setting this property to an enumerable, the view will display each item in the collection. 通过将此属性设置为可枚举,视图将显示集合中的每个项目。

By default, it will just render each item as a textblock showing the result of calling ToString() on each object. 默认情况下,它将仅将每个项目呈现为文本块,显示在每个对象上调用ToString()的结果。 There are several ways of customizing this. 有几种自定义方式。 In your case, you have defined columns with a GridView and GridViewColumn s. 在您的情况下,您已使用GridViewGridViewColumn定义了列。 Each GridViewColumn has a DisplayMemberBinding which can be binded to the property you want to display in that column. 每个GridViewColumn都有一个DisplayMemberBinding ,可以将其绑定到要在该列中显示的属性。

So... 所以...

I'm not sure how easy it will be for you to use this. 我不确定您使用它的难易程度。 As mentioned by others, you really should learn a little bit about binding in wpf, and the Model-View-ViewModel (MVVM) pattern. 正如其他人所提到的,您确实应该学习一些有关wpf中的绑定以及Model-View-ViewModel(MVVM)模式的知识。 MVVM really helps keeping the code clean. MVVM确实有助于保持代码干净。

Anyways... 无论如何...

Your view could be changed to something like this: 您的视图可以更改为以下形式:

<ListView x:Name="ListView1" HorizontalAlignment="Left" Height="375" Margin="344,23,0,0" VerticalAlignment="Top" Width="567" ItemsSource="{Binding patients}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="First Name" Width="100" DisplayMemberBinding="{Binding path=firstname}" />
                <GridViewColumn Header="Last Name" Width="80" DisplayMemberBinding="{Binding path=lastname}" />
                <GridViewColumn Header="Address" Width="100" DisplayMemberBinding="{Binding path=Address}" />
                <GridViewColumn Header="Security Type" Width="80" DisplayMemberBinding="{Binding path=securityType}" />
                <GridViewColumn Header="Gender" Width="100" DisplayMemberBinding="{Binding path=gender}" />
                <GridViewColumn Header="Age" Width="100" DisplayMemberBinding="{Binding path=age}" />
            </GridView>
        </ListView.View>
    </ListView>

I would change your fixed size array of patients to a ObservableCollection<Patient> . 我会将患者的固定大小数组更改为ObservableCollection<Patient> A collection that can grow in size is almost always better than a fixed size one. 可以增长大小的集合几乎总是比固定大小的集合要好。 The ObservableCollection<> has some extra tricks as well. ObservableCollection<>也有一些额外的技巧。 It will notify the view whenever items are added or removed. 每当添加或删除项目时,它将通知视图。

Take a look at wpftutorial.net . 看一下wpftutorial.net You will find a nice introduction to binding , MVVM and a lot more. 您将找到绑定MVVM等的不错介绍

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

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