在 wpf 中将文本框中的数据显示到列表视图中 [英] Show data from textboxes into listview in wpf

查看:30
本文介绍了在 wpf 中将文本框中的数据显示到列表视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 项目:

I have a C# project have :

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>

我有一个班级病人:

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

和主程序

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

推荐答案

将数据放入列表视图的正常方式是通过数据绑定.数据绑定是 WPF 用于在视图和代码之间传输数据的方式.

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 控件,如列表视图、列表框、组合框等,都有一个 ItemSource 属性.通过将此属性设置为可枚举,视图将显示集合中的每个项目.

All wpf controls that can show multiple items, like listview, listbox, combobox etc, has a ItemSource property. By setting this property to an enumerable, the view will display each item in the collection.

默认情况下,它只会将每个项目呈现为一个文本块,显示对每个对象调用 ToString() 的结果.有多种自定义方式.在您的情况下,您已经使用 GridViewGridViewColumn 定义了列.每个 GridViewColumn 都有一个 DisplayMemberBinding,它可以绑定到要在该列中显示的属性.

By default, it will just render each item as a textblock showing the result of calling ToString() on each object. There are several ways of customizing this. In your case, you have defined columns with a GridView and GridViewColumns. Each GridViewColumn has a DisplayMemberBinding which can be binded to the property you want to display in that column.

所以...

我不确定你使用它会有多容易.正如其他人所提到的,您确实应该了解一些有关 wpf 中的绑定以及模型-视图-视图模型 (MVVM) 模式的知识.MVVM 确实有助于保持代码清洁.

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. MVVM really helps keeping the code clean.

无论如何...

您的视图可以更改为如下所示:

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>

我会将您固定大小的患者数组更改为 ObservableCollection.可以增加大小的集合几乎总是比固定大小的集合好.ObservableCollection<> 也有一些额外的技巧.每当添加或删除项目时,它都会通知视图.

I would change your fixed size array of patients to a 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. It will notify the view whenever items are added or removed.

看看 wpftutorial.net.你会发现一个很好的 绑定介绍 , MVVM 等等.

Take a look at wpftutorial.net. You will find a nice introduction to binding , MVVM and a lot more.

这篇关于在 wpf 中将文本框中的数据显示到列表视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆