0

我有一个 C# 项目:

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


    }
}

所以我想在列表视图中显示在文本框、单选按钮和组合框中输入的数据的问题...

4

1 回答 1

1

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

所有可以显示多个项目的 wpf 控件,如列表视图、列表框、组合框等,都有一个ItemSource属性。通过将此属性设置为可枚举,视图将显示集合中的每个项目。

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

所以...

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

无论如何...

您的视图可以更改为以下内容:

<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<Patient>. 一个可以增长的集合几乎总是比一个固定大小的集合好。也有一些额外的ObservableCollection<>技巧。每当添加或删除项目时,它都会通知视图。

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

于 2013-06-05T20:24:42.650 回答