Reputation: 4122
I know this question may sound similar to my last one but this is a different approach taking from others advice. Going a different route I'm trying to bind whats in a listview to a couple of textboxes. I am extremely new to WPF's and Not sure if I'm doing this right. I guess this a MVVP approach. Right now the program doesn't do much. There is one item in the listview but it is empty(No name, age, grade). I can see the item b/c it highlights when I try to select it on the listview. Any help is greatly appreciated.
There is class that makes a string name, int age ,int grade and has getter and setter methods. Name of class is called Camper(age, grade, name)
Class BindingCamper: making observablecollection
public class Camper
{
public Camper[] requests;
public int[] relationValues;
public String name;
private String school;
public int age;
public int grade;
private Boolean isGrouped = false;
private string group;
public Camper(int a, int g, String n)
{
requests = new Camper[4];
relationValues = new int[4];
name = n;
this.age = a;
this.grade = g;
}
public Camper(String n)
{
this.requests = new Camper[4];
this.relationValues = new int[4];
this.name = n;
}
public Camper()
{
}
// Getter Methods
public string getName()
{
return name.ToString();
}
public string getSchool()
{
return this.school;
}
public int getAge()
{
return this.age;
}
public int getGrade()
{
return this.grade;
}
public int getRelationValue(int i)
{
if (i < relationValues.Length)
{
return relationValues[i];
}
else
{
return 0;
}
}
class BindingCamper
{
public ObservableCollection<Camper> Campers { get; private set; }
public BindingCamper()
{
Campers = new ObservableCollection<Camper>();
}
Another class(page) :
public partial class CampersPage : Page
{
MainWindow _parentForm;
public ObservableCollection<Camper> Campers { get; private set; }
public CampersPage(MainWindow parent)
{
_parentForm = parent;
InitializeComponent();
// Campers = new ObservableCollection<Camper>();
var bindMe = new BindingCamper();
Camper CampMe = new Camper(3, 4, "Tony Lagarrigue");
// CampMe.getName();
bindMe.Campers.Add(CampMe);
DataContext = bindMe;
XAML to bind everything. :
<ListView HorizontalAlignment="Left" Margin="10,10,0,40" x:Name ="listViewCampers" Width="200" SelectionChanged="listViewCampers_SelectionChanged" ItemsSource="{Binding Campers}" DisplayMemberPath="name" IsSynchronizedWithCurrentItem="{x:Null}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="40" />
<GridViewColumn Header="Grade" DisplayMemberBinding="{Binding Grade}" Width="40" />
</GridView>
</ListView.View>
</ListView>
<Grid Height="Auto" HorizontalAlignment="Stretch" Margin="209,12,0,0" Name="infoGrid" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="134*" />
<RowDefinition Height="154*" />
</Grid.RowDefinitions>
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="23,24,0,0" Name="lblName" VerticalAlignment="Top" />
<TextBox Text="{Binding name, ElementName=listViewCampers}" Height="23" HorizontalAlignment="Left" Margin="23,46,0,0" Name="txtName" VerticalAlignment="Top" Width="120" AcceptsReturn="True" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="23,103,0,0" Name="txtAge" VerticalAlignment="Top" Width="120" />
Upvotes: 3
Views: 1451
Reputation: 28046
For this code in your ListView declaration:
DisplayMemberPath="name"
you are using the field name instead of the property. I don't think that databinding works against fields, even if they are public (to be honest, I have never understood why this is the case).
It looks like you are doing this in a few other spots, too--make sure you are binding to the property, and make sure the name is correct (it is case sensitve).
Edit: as SLaks noted (I didn't notice this until he pointed it out), you are using methods as properties. I think they need to be actual .Net properties with getters, e.g.:
public string Name
{
get { return _name; }
}
Upvotes: 1
Reputation: 888203
You need to create public properties in your viewmodel to bind to.
You should never make getSomething()
methods in .Net.
Upvotes: 4