日期:2014-05-17  浏览次数:20907 次

WPF,DataGrid数据绑定不是双向绑定也能更新?

<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 = "化学" });