0
votes

Let's say I have a Person Class like

Public class Person
{

public string firstName {set;get;}
public string lastName {set;get;}
}

in my ViewMode class I have an observableCollection of Person

 private ObservableCollection<Person> _people;
 public ObservableCollection<Person> People
    {
        get { return _people; }
        set
        {
            _people= value;
            FirePropertyChanged("People");
        }
    }

in my view I have a datagrid that ItemsSource is binded to People. I have a combobox in my view that its SelectedValue is binded to SelectedFieldValue and I want to add a row to my gridview with the value selected in this Combobox

  private string _selectedFieldValue;
    public string SelectedFieldValue
    {
        get { return _selectedFieldValue; }
        set
        {
            _selectedFieldValue = value;

            FirePropertyChanged("SelectedFieldValue");
            People.Add(New Person{firstName = value});

         }
    }

the row is added to the Grid, but I don't see the value of the FirstName added. I tried several way to bind the column FirstName but couldn't get it working correctly.What's the correct way to bind my DataGridTextColumn in grid to firstName, lastName properties of the person class.

1

1 Answers

1
votes

check out this..

 <sdk:DataGrid x:Name="dataGrid4"  
        Height="160" ItemsSource="{Binding People}" AutoGenerateColumns="False" >
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn 
                Header="First Name" 
                Width="SizeToHeader"
                Binding="{Binding firstName}" 
                FontSize="20" />
            <sdk:DataGridTextColumn 
                Header="Last Name" 
                Width="SizeToCells"
                Binding="{Binding lastName}" 
                FontSize="20" />
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>