Reputation: 1
I have a C# project have :
The XAML Code:
<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
Upvotes: 0
Views: 1373
Reputation: 12898
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.
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.
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 GridViewColumn
s. Each GridViewColumn
has a DisplayMemberBinding
which can be binded to the property you want to display in that column.
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. MVVM really helps keeping the code clean.
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>
. 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.
Take a look at wpftutorial.net. You will find a nice introduction to binding , MVVM and a lot more.
Upvotes: 1