我有一個ListBox和DataGrid。如何根據列表框選擇更新DataGrid的ItemsSource
我想在更改ListBox中的選擇時更改DataGrid的ItemsSource。 示例:如果我選擇ListBox中的第一個項目,Collection1應綁定到DataGrid的ItemsSource。如果我選擇第二個項目Collection2應綁定到DataGrid的ItemsSource。
下面是我試過
視圖 - XAML中
<ListBox
Grid.Column="0"
Width="100"
ItemsSource="{Binding coll1}"
SelectedItem="{Binding SelectedName}">
</ListBox>
<DataGrid
Grid.Column="1"
ItemsSource="{Binding coll, Mode=TwoWay}">
</DataGrid>`
視圖模型 -
public class VM1
{
private List<Employee> _coll = new List<Employee>();
public List<Employee> coll
{
get
{
return _coll;
}
set
{
coll = value;
}
}
public List<string> _coll1 = new List<string>();
public List<string> coll1
{
get
{
return _coll1;
}
}
private string _selectedName = "";
public string SelectedName
{
get
{
return _selectedName;
}
set
{
_selectedName = value;
}
}
public VM1()
{
_coll.Add(new Employee());
_coll.Add(new Employee());
_coll.Add(new Employee());
_coll1.Add("One");
_coll1.Add("Two");
_coll1.Add("Three");
}
}
型號 -
public class Employee
{
public Employee()
{
Name = "Hello";
}
public string Name { get; set; }
}
查找INotifyPropertyChanged的作爲用於綁定更新通知的根。 –
什麼是'Collection1'和'Collection2'? –