在WPF中写入DataGrid的ComboBoxColumn

时间:2016-01-28 09:06:21

标签: c# wpf datagridcomboboxcolumn

我希望有可能写一个不在DatagridComboBoxColumn

列表中的值

我怎么能这样做?

WinForm我以前用

做的
private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox c = e.Control as ComboBox;
            if (c != null) c.DropDownStyle = ComboBoxStyle.DropDown;
        }
private void DataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {

            if (e.ColumnIndex == dataGridViewTextBox.Index)
            {
                object eFV = e.FormattedValue;
                if (!dataGridViewTextBox.Items.Contains(eFV))
                {

                    DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;


                }
            }
        }

1 个答案:

答案 0 :(得分:1)

可以使用TargetNullValueFallbackValue

<DataGrid>
   <DataGridComboBoxColumn ItemsSource="{Binding EmployeeNames,
TargetNullValue='Oops, I am missing',FallbackValue='I am default'}"/>        
</DataGrid>

正如MSDN所说:

  

FallbackValue - 获取或设置绑定时要使用的值   无法返回值。 TargetNullValue - 获取或设置   当源的值为null时,在目标中使用的值。

<强>更新

视图模型:

private ObservableCollection<Person> persons;

public ObservableCollection<Person> Persons
{
    get { return persons; }
    set { persons = value;
         OnPropertyChanged("Persons");
    }
}

private void FillData()
{
   persons = new ObservableCollection<Person>();
   for (int i = 0; i < 30; i++)
   {
      if(i%2==0)
         persons.Add(new Person() { IdPerson=i, Name="", SurName="Albahari"});
      else
          persons.Add(new Person() { IdPerson = i, Name = "Ben & Joseph " + i.ToString(), SurName = "Albahari" });
   }
   //If you want set some value, you can do it:
   if(persons[0].Name=="")
      persons[0].Name = "That should be Name";

}