XamDataGrid 根据列表框中的选定项进行绑定

XamDataGrid Bind based on selected item in listbox

我正在尝试根据列表框中的选定项目加载数据网格。

你能帮我实现一下吗

下面是我的模型

艺术家Class

public class Artist.
{
public string Name { get; set; }
public int Age { get; set; }
}

电影Class

public class Movie
{
private string _Name = string.Empty;

public string Name
{
get
{
return _Name;
}

set
{
_Name = value;
}
}

private List _Artists = new List();

public List Artists
{
get
{
return _Artists;
}
set
{
_Artists = value;
}
}
}

视图模型Class

public class ViewModel
{

private List _Movies;

public List Movies
{
get
{
return _Movies;
}

set
{
_Movies = value;
}
}

public ViewModel()
{

_Movies = new List();

Movie movie = new Movie();
movie.Name = "Movie 1";

List artists = new List();

Artist artist1 = new Artist();
artist1.Name = "Artist 1";
artist1.Age = 27;

artists.Add(artist1);

movie.Artists = artists;

_Movies.Add(movie);

//2nd record

movie = new Movie();
movie.Name = "Movie 2";

artists = new List();

artist1 = new Artist();
artist1.Name = "Artist 2";
artist1.Age = 27;

artists.Add(artist1);

movie.Artists = artists;

_Movies.Add(movie);

}
}

下面是 Xaml - ViewModel 对象绑定到 window datacontext

<Grid>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<ListBox Name="MoviesList" Grid.Column="0" Width="150" ItemsSource="{Binding Movies}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

<igWPF:XamDataGrid Grid.Column="1" DataSource="{Binding Movies}">
<igWPF:XamDataGrid.FieldLayouts>
<igWPF:FieldLayout>
<igWPF:Field Name="Name"/>
</igWPF:FieldLayout>
</igWPF:XamDataGrid.FieldLayouts>
</igWPF:XamDataGrid>
</Grid>

如果要在 DataGrid 中显示所选电影的艺术家,请尝试此操作:

<igWPF:XamDataGrid Grid.Column="1" DataSource="{Binding SelectedItem.Artists, ElementName=MoviesList}">