<Grid>
<DataGrid Name="datagrid1" Margin="123,146,553,42" AutoGenerateColumns="False" Height="131" Width="244">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" Width="105" Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="年龄" Binding="{Binding Path=Age}" />
<DataGridComboBoxColumn x:Name="dataGridComboBoxColumn1" SelectedItemBinding="{Binding Path=aaa}" Header="科目" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Button" HorizontalAlignment="Left" Margin="292,102,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<DataGrid x:Name="datagrid2" Margin="429,146,247,42" AutoGenerateColumns="False" Height="131" Width="244">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" Width="105" Binding="{Binding Name}" />
<DataGridTextColumn Header="年龄" Binding="{Binding Age}" />
<DataGridComboBoxColumn x:Name="dataGridComboBoxColumn2" SelectedItemBinding="{Binding aaa}" Header="科目" />
</DataGrid.Columns>
</DataGrid>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
datagrid1.ColumnWidth = DataGridLength.Auto;
ObservableCollection<Person> obc = new ObservableCollection<Person>();
obc.Add(new Person() { Name = "张三", Age = 43, Address = "上海", aaa = "语文" });
obc.Add(new Person() { Name = "李四", Age = 42, Address = "北京", aaa = "数学" });
obc.Add(new Person() { Name = "王二女", Age = 50, Address = "美国", aaa = "化学" });
datagrid1.ItemsSource = obc;
datagrid2.ItemsSource = obc;
List<string> list = new List<string>() { "语文", "数学", "化学", "物理" };
dataGridComboBoxColumn1.ItemsSource = list;
dataGridComboBoxColumn2.ItemsSource = list;
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string aaa { get; set; }
}
ObservableCollection已包括了INotifyPropertyChanged 接口
datagrid1.ItemsSource = obc;
datagrid2.ItemsSource = obc;
两个同时绑定一个源哪有不同时更改的道理?
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。