Datagrid双向绑定不起作用

时间:2015-02-02 13:49:40

标签: c# wpf datagrid

我有一个数据网格,xaml就像:

<DataGrid HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="9.75,15" Margin="19,119,0,0" Height="359" Width="1140" Name="TeamsGrid"  AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding sourceId}" IsReadOnly="True" Header="Kaynak takım id" />
        <DataGridTextColumn Binding="{Binding sourceName}"  IsReadOnly="True" Header="Kaynak takım adı"/>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Click="btnSelectTeam">...</Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Binding="{Binding BsdId}" Header="BSD Takım id" IsReadOnly="True"/>
        <DataGridTextColumn Binding="{Binding BsdName}" Header="BSD Takım adı" IsReadOnly="True"/>
        <DataGridCheckBoxColumn Binding="{Binding SaveMapping, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Kaydet" />
    </DataGrid.Columns>
</DataGrid>

我试图将其绑定到列表中。

private ObservableCollection<Team> TeamList;
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (UrlBox.Text.Contains("something"))
    {
        var mapper = new TeamMapper();
        TeamList = new ObservableCollection<Team>(mapper.MapTeams(UrlBox.Text));
    }
}

但是当我填充集合时,数据不会出现在datagrid上。所以我在该代码中添加了一行:

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (UrlBox.Text.Contains("something"))
    {
        var mapper = new TeamMapper();
        TeamList = new ObservableCollection<Team>(mapper.MapTeams(UrlBox.Text));
        TeamsGrid.ItemsSource = TeamList;
    }
}

使用此代码,数据会出现在datagrid上,但是当我在gridview上选中一个复选框时,收集不会更新。

编辑:

我将datagrid xaml更改为:

<DataGrid HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="9.75,15" Margin="19,119,0,0" Height="359" Width="1140" Name="TeamsGrid"  AutoGenerateColumns="False" ItemsSource="{Binding TeamList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}">

1 个答案:

答案 0 :(得分:3)

正确的方法是:

首先您的ViewModel必须实现INotifyPropertyChanged接口,如果集合属性发生任何更改,DataGrid将被通知

第二次DataGrid ItemSource属性绑定到您的收藏集,

第三次将Window DataContext设置为您的视图模型(使用this.DataContext=this或Xaml的代码,如下所示:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

xaml:

<DataGrid HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="9.75,15" Margin="19,119,0,0" Height="359" Width="1140"
ItemsSource="{Binding TeamList, Mode=TwoWay}" Name="TeamsGrid"  AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding sourceId}" IsReadOnly="True" Header="Kaynak takım id" />
        <DataGridTextColumn Binding="{Binding sourceName}"  IsReadOnly="True" Header="Kaynak takım adı"/>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Click="btnSelectTeam">...</Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Binding="{Binding BsdId}" Header="BSD Takım id" IsReadOnly="True"/>
        <DataGridTextColumn Binding="{Binding BsdName}" Header="BSD Takım adı" IsReadOnly="True"/>
        <DataGridCheckBoxColumn Binding="{Binding SaveMapping, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Kaydet" />
    </DataGrid.Columns>
</DataGrid>

并更新您的ViewModel /代码:

 public partial class MainWindow : Window,INotifyPropertyChanged
{ 

    private ObservableCollection<Team> _teamList  ;

    public ObservableCollection<Team> TeamList
    {
        get
        {
            return _teamList;
        }

        set
        {
            if (_teamList == value)
            {
                return;
            }

            _teamList = value;
            OnPropertyChanged();
        }
    }


    public MainWindow()
    {
        InitializeComponent();

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
      if (UrlBox.Text.Contains("something"))
      {
        var mapper = new TeamMapper();
        TeamList = new ObservableCollection<Team>    (mapper.MapTeams(UrlBox.Text));
     }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }


}