Straight to the point. I'm writing a program using MVVM and I have made a view like this:
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 ?
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.