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

时间:2017-06-01 10:25:03

标签: c# wpf xamdatagrid

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

你能帮助我如何实现。

以下是我的模特

艺术家班

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

电影课

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;
}
}
}

ViewModel 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对象绑定到窗口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>

1 个答案:

答案 0 :(得分:1)

如果要在DataGrid

中显示所选电影的艺术家,请尝试此操作
<igWPF:XamDataGrid Grid.Column="1" DataSource="{Binding SelectedItem.Artists, ElementName=MoviesList}">