Binding a list with another list with WPF

2019-07-20 19:35发布

Straight to the point. I'm writing a program using MVVM and I have made a view like this:

my View

Class structure:

class Company    
{
    int CompanyID
    string Name
    List<Material> MaterialList
}

class Material
{
    int ID
    string Name
    string Description
}

Here is the XAML code of my View (deleted most of the irrelevant stuff to make it more readable):

<ListView x:Name="_companies" ItemsSource="{Binding ElementName=_this, Path=ItemsSource}" SelectedItem="{Binding ElementName=_this, Path=SelectedItem}">
       <ListView.View>
            <GridView>
                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding CompanyID}" />
                <GridViewColumn Header="Company Name" DisplayMemberBinding="{Binding CompanyName}" />
            </GridView>

...

    <ListView x:Name="_materials" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElementName=_companies, Path=SelectedItem.MaterialID}"/>
                <GridViewColumn  Header="Name" DisplayMemberBinding="{Binding ElementName=_companies, Path=SelectedItem.Name"/>
            </GridView>
        </ListView.View>
    </ListView>

    <TextBox x:name="_description" Text="{Binding Description}" IsReadOnly="True" />
</StackPanel>

And a part of my MainView:

<Window.DataContext>
    <viewModels:CompanyListViewModel />
</Window.DataContext>
<Grid>
<view:CompanyListView ItemsSource="{Binding Companies}" />

Companies is a list of Company objects containing Name, ID and MaterialList. The Companies list is displayed in the _companies ListView (code above).

Now after selecting a company from the list I want to display the assigned MaterialList in the _materials ListView.

After selecting a material in _materials I want to display its description in the _description TextBox

How do I do that? I found a similar thread that explains the concept but I still can't do it in my case. Can I bind to one of the SelectedItem properties ?

标签: c# wpf xaml mvvm
1条回答
男人必须洒脱
2楼-- · 2019-07-20 20:17

Refer the below code. I have done it using MVVM. You need use SelectedItem property of the listview and bind to the chid data listview.

<StackPanel>
    <ListView x:Name="_companies" ItemsSource="{Binding Companies}" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" />
                <GridViewColumn Header="Company Name" DisplayMemberBinding="{Binding Name}" />
            </GridView>
        </ListView.View>
    </ListView>
    <ListView x:Name="_materials" ItemsSource="{Binding ElementName=_companies,Path=SelectedItem.MaterialList}" >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
                <GridViewColumn  Header="Name" DisplayMemberBinding="{Binding Name}"/>
            </GridView>
        </ListView.View>
    </ListView>
    <TextBox x:Name="_description" Text="{Binding ElementName=_materials, Path=SelectedItem.Description}" IsReadOnly="True" />
</StackPanel>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainViewModel();
    }
}
  class MainViewModel 
{      

    private ObservableCollection<Company> companies;

    public ObservableCollection<Company> Companies
    {
        get { return companies; }
        set { companies = value; }
    }        

    public MainViewModel()
    {
        companies = new ObservableCollection<Company>();

        for (int i = 0; i < 10; i++)
        {
            Company comp = new Company();
            comp.ID = i + 1;
            comp.Name = "Comp" + i;
            ObservableCollection<Material> matlist = new ObservableCollection<Material>();
            for (int j = 0; j < 10; j++)
            {
                Material mat = new Material();
                mat.ID = j + 1;
                mat.Name = "Mat" + j + i;
                mat.Description = "descrp" + j + i;
                matlist.Add(mat);
            }
            comp.MaterialList = matlist;
            companies.Add(comp);
        }            
    }
}

class Company 
{
    private int id;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value;  }
    }
    private ObservableCollection<Material> materialList;

    public ObservableCollection<Material> MaterialList
    {
        get { return materialList; }
        set { materialList = value; }
    }       
}

class Material 
{
    private int id;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string description;

    public string Description
    {
        get { return description; }
        set { description = value; }
    }
}
查看更多
登录 后发表回答