List of items from an object selected in another list

Well the name can be a little confusing. I have a database with a table. Companies that have a one-to-many switch with a different Sections table (so each company can have many departments), and the department will have many employees.

I have a listview of companies. I do not want that when I select a company from ListView, it includes another ListView of divisions within this company. Then I select a unit and another list of employees within these units, which are lower than this. You get an image.

Is there a way to do this mostly inside the XAML code declaratively (sp?). I use linq, so Company objects have a property called Division, which, if I understand correctly, that linq should include Division objects of the divisions associated with the company. Therefore, having received all the companies and setting them as the data source for CompanyListView, I am now.

   <ListView x:Name="CompanyListView"
              DisplayMemberPath="CompanyName"
              Grid.Row="0" Grid.Column="0" />

    <ListView DataContext="{Binding ElementName=CompanyListView, Path=SelectedItem}"
              DisplayMemberPath="Division.DivisionName"
              Grid.Row="1" Grid.Column="0" />

I know I'm leaving, but I was hoping that by putting something specific in the DataContext and DisplayMemberPath, I could get this to work. If not, then I have to capture the identifier of the company I am assuming and capture the selected event or something else.

, seconde, lisview, / . , , , , , , , - ?

+3
2

MVVM XAML , ListViews reset , Comany.

, .

ListView XAML:

<Window x:Class="MultiListView.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="400" Width="800">
  <DockPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0" 
                  ItemsSource="{Binding Companies}"
                  SelectedItem="{Binding Company, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                                    DisplayMemberBinding="{Binding CompanyName}" />
                    <GridViewColumn Header="Description"
                                    DisplayMemberBinding="{Binding Description}" />
                </GridView>
            </ListView.View>
        </ListView>
        <ListView Grid.Row="1" 
                  ItemsSource="{Binding Divisions}"
                  SelectedItem="{Binding Division, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                                    DisplayMemberBinding="{Binding DivisionName}" />
                    <GridViewColumn Header="Description"
                                    DisplayMemberBinding="{Binding Description}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
  </DockPanel>
</Window>

, DataContext Window , , XAML.

public partial class MainView : Window
{
   MainViewModel _mvm = new MainViewModel();

   public MainView()
   {
      InitializeComponent();
      DataContext = _mvm;
   }
}

MVVM, StackOverFlow. , XAML. LINQ / .

using System.Collections.ObjectModel;
using MultiListView.Models;

namespace MultiListView.ViewModels
{
public class MainViewModel : ViewModelBase
{
  public MainViewModel()
  {
     _companies = new ObservableCollection<Company>();
     _companies.Add(new Company("Stackoverflow", "QA web site"));
     _companies.Add(new Company("Fog Creek", "Agile Bug Tracking"));
     _companies.Add(new Company("Second Beach", "Not sure yet"));

     _divisions = new ObservableCollection<Division>();
  }

  private ObservableCollection<Company> _companies;
  public ObservableCollection<Company> Companies
  {
     get { return _companies; }
     set
     {
        _companies = value;
        OnPropertyChanged("Companies");
     }
  }

  private Company _company;
  public Company Company
  {
     get { return _company; }
     set
     {
        _company = value;

        // load/reload divisions for the selected company here
        LoadDivisions();

        OnPropertyChanged("Company");
     }
  }

  // hack to keep the example simpe...
  private void LoadDivisions()
  {
     _divisions.Clear();

     // use db or linq here to filiter property
     if ( _company != null )
     {
        if ( _company.CompanyName.Equals("Stackoverflow") )
        {
           _divisions.Add( new Division("QA", "Test all day"));
           _divisions.Add( new Division("Write", "Doc all day"));
           _divisions.Add( new Division("Code", "Code all day"));
        }
        else if (_company.CompanyName.Equals("Fog Creek"))
        {
           _divisions.Add(new Division("Test", "Test all day"));
           _divisions.Add(new Division("Doc", "Doc all day"));
           _divisions.Add(new Division("Develop", "Code all day"));
        }
        else if (_company.CompanyName.Equals("Second Beach"))
        {
           _divisions.Add(new Division("Engineering", "Code all day"));
        }
     }
  }

  private ObservableCollection<Division> _divisions;
  public ObservableCollection<Division> Divisions
  {
     get { return _divisions; }
     set
     {
        _divisions = value;
        OnPropertyChanged("Divisions");
     }
  }

  private Division _division;
  public Division Division
  {
     get { return _division; }
     set
     {
        _division = value;
        OnPropertyChanged("Division");
     }
  }
}
}

OnPropertyChanged INotifyPropertyChanged.
ViewModel , , ViewModel, , ViewModel PropertyChanged.

MVVM MSDN .

+2

Divisions , , , - :

    <Window x:Class="MultiListView.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="400" Width="800">
  <DockPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0" 
                  x:Name="lvCompanies"
                  ItemsSource="{Binding Companies}"
                  SelectedItem="{Binding Company, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                                    DisplayMemberBinding="{Binding CompanyName}" />
                    <GridViewColumn Header="Description"
                                    DisplayMemberBinding="{Binding Description}" />
                </GridView>
            </ListView.View>
        </ListView>
        <ListView Grid.Row="1" 
                  ItemsSource="{Binding ElementName='lvCompanies', Path=SelectedItem.Divisions}"
                  SelectedItem="{Binding Division, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                                    DisplayMemberBinding="{Binding DivisionName}" />
                    <GridViewColumn Header="Description"
                                    DisplayMemberBinding="{Binding Description}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
  </DockPanel>
</Window>
+3

Source: https://habr.com/ru/post/1750124/


All Articles