2016-01-28 55 views
0
一個ComboBoxColumn

我想有寫另一個值是不在列表中的可能性DatagridComboBoxColumn寫在DataGrid中的WPF中

我怎麼能這樣做呢?

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

你嘗試過什麼了嗎? – Muds

+0

其實也沒什麼,我不知道從哪裏開始,我沒有與XAML設計非常好。 – Cantinou

回答

1

它可以使用TargetNullValueFallbackValue做到這一點:

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

作爲MSDN說:

FallbackValue - 獲取或設置值當綁定是 無法返回值時使用。 TargetNullValue - 獲取獲取或設置在目標使用時,源的值是空的 值。

更新:

視圖模型:

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"; 

}

+0

好的,我會看看這種方式。謝謝 – Cantinou

+0

但是我怎樣才能設置默認值?因爲我想寫我想要的而不是默認值。 – Cantinou

+0

它是你的應用程序代碼還是MVVM? – StepUp