Diyan Bogdanov
Diyan Bogdanov

Reputation: 77

WPF ListView binding from another listview

First view my database - LINK and my ListView Binding

In first ListView i showed "TestSubjectName" from table "TestSubjectsNames" and when i select item to show in second ListView "TestName" from tabel "Tests", where "TestSBJNameID" == "ID" from selected item in first ListView.

This is part from my code about second ListView but it is not correct.

    private IEnumerable<Tests> testsNames;

    public IEnumerable<Tests> TestsNames
    {
        get
        {
            if (this.testsNames== null)
            {
                this.testsNames= BindingData.GetAllTestsNames;
            }
            return this.testsNames;
        }
    }

    public static IEnumerable<Tests> GetAllTestsNames
    {
        get
        {
            return new Model().TestsNames.Where(t => t.ID == FIRST-LISTVIEW-SELECTED-INDEX).Select(c => new Tests 
            {
                testNameID = c.ID,
                testName = c.TestsNames                   
            });
        }            
    }

If anyone have any other solution how from selecting in first ListView to show item in second ListView please tell me.

Upvotes: 0

Views: 1112

Answers (1)

Aurelien Souchet
Aurelien Souchet

Reputation: 721

I think there is a lot of ways to do what you want, this is what I would do :

In the CodeBehind

public class TestWindow
{

    // A collection of SubjectNames to bind with the first listview
    private ObservableCollection<TestsSubjectsNames> _subjectNames;
    public ObservableCollection<TestsSubjectsNames> SubjectNames
    {
        get { return _subjectNames; }
        set
        {
           _subjectNames = value;
           NotifyPropertyChanged("SubjectNames");
        }
    }

    // A collection of Test for the second listview
    private ObservableCollection<Tests> _testNames;
    public ObservableCollection<Tests> TestNames
    {
        get { return _testNames; }
        set
        {
           _testNames= value;
           NotifyPropertyChanged("TestNames");
        }
    }

    // The current selected TestsSubjectsNames in first listview
    private TestsSubjectsNames _selectedSubjectNames;
    public TestsSubjectsNames SelectedSubjectNames
    {
        get { return _selectedSubjectNames; }
        set
        {
           _selectedSubjectNames= value;
           // Call this function in the setter
           // Will change the Collection binded to the second listview
           OnSelectedSubjectNameChanged(value);
           NotifyPropertyChanged("SelectedSubjectNames");
        }
    }

    public TestWindow()
    {
        // Initialization and set datacontext
        InitializeComponent();
        DataContext = this;

        // Initialize the suject name list for the first listview
        SubjectNames = GetAllSubjectNames();

        // Second listview empty until we select something in the first list
        // so initialize it empty
        TestNames = new ObservableCollection<Tests>();
    }

    // Will be called everyTime the selectedItem in the first listview changes
    private void OnSelectedSubjectNameChanged(TestsSubjectsNames subjectName)
    {
          // Get The list of Tests with the selected subjectName ID
          // and put the result in the observable collection
          TestNames = new ObservableCollection<Tests>(GetAllTestsNames(subjectName.ID))
    }
}

Now you just Have to bind correctly in the XAML

First Listview :

 // SelectedItem attribute very important
 <ListView  
      ItemsSource="{Binding SubjectNames}" 
      SelectedItem="{Binding SelectedSubjectNames}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                // show a textblock with the TestsSubjectName property
                // of TestsSubjectsNames class 
                <TextBlock Text="{Binding TestsSubjectName}" />
            </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>

Second Listview

 <ListView ItemsSource="{Binding TestNames}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                // show a textblock with the TestName property
                // of Tests class 
                <TextBlock Text="{Binding TestName}" />
            </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>

In the first ListView we binded SelectedItem with SelectedSubjectNames, and everytime SelectedSubjectNames is set, we call the function OnSelectedSubjectNameChanged who changes the TestNames Collection.

Because TestNames is binded as itemsSource in the second ListView, everyTime you select a different subject name, the list of test names will appear in the second listview.

Hope it was clear enough and answered your question.

Upvotes: 1

Related Questions