0

So I'm trying to learn the MVVM design patter in WPF, I want to do the following:

In external class I've got a ObservableCollection _students that is bound to a listview on the WPF window using MVVM design pattern. The listview shows only the Student's name and Age.

public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Course { get; set; }
    public DateTime JoiningDate { get; set; }
}


public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Student> _students;

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged!=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public ObservableCollection<Student> Students
    {
        get
        {
            return _students;
        }
        set
        {
            _students = value;
            NotifyPropertyChanged("Students");
        }
    }

All good, but I want to put a TextBox and set it to show the listview's selected item's course property. This means I must

  1. get the listview's selected index (ok)
  2. bind the textbox.Text property to Students[that index].Course

I'm stuck at 2. Any help?

1
  • Not sure of the syntax but you could bind the TextBox to the SelectedValue of the ListBox <TextBox Text="{Binding ElementName=StudentsList, Path=SelectedValue.Course}"/> - though I don't think this will work. Commented Nov 2, 2012 at 10:18

2 Answers 2

1

i would solve this by another way.

Take a look at this post .

Another way would be that your ViewModel contains a Student-property(e.g. SelectedStudent) which is bind to the SelctedItem of the listView. Then you can handel this by

{Binding Path=SelectedStudent.Course}
Sign up to request clarification or add additional context in comments.

Comments

1

Assume you bind the listview to a collection of type SampleData like below:

SampleData.cs

public class SampleData
{
    public int Id { get; set; }

    public string Text { get; set; }

    public decimal Value { get; set; }
}

Then you bind the ListView ItemsSource to a collection. WIt does not matter if you bind ItemsSource property to a property on ViewModel or you bind it in code-behind like the code below.

var source = new List<SampleData>();

source.Add(new SampleData() { Id = 1, Text = "AAA" });
source.Add(new SampleData() { Id = 2, Text = "BBB" });
source.Add(new SampleData() { Id = 3, Text = "CCC" });
source.Add(new SampleData() { Id = 4, Text = "DDD" });
source.Add(new SampleData() { Id = 5, Text = "EEE" });

You can bind TextBox's Text property to the SelectedItem directly on the View.

<StackPanel Orientation="Horizontal">
    <ListView x:Name="listView1" />

    <TextBox Text="{Binding ElementName=listView1, Path=SelectedItem.Text}" />
</StackPanel>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.