2016-11-11 143 views
0

我想在動態組合框中選擇1項,因爲我在DataGrid行中有很多組合框。我嘗試使用SelectedValue或SelectedIndex,但它仍然無法正常工作。請幫幫我 。我的代碼在這裏在組合框中動態設置選定的項目wpf

XAML文件:

<DataGridTemplateColumn Header="DataType_Id"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <ComboBox Loaded="cbxDataType_Loaded" Name="cbxDataType" SelectionChanged="ComboBox_SelectionChanged" 
        SelectedValuePath="Id" 
         DisplayMemberPath="Name" 
         ItemsSource="{Binding Path=masterData}" 
         SelectedValue="{Binding Path=ComboboxObj,Mode=TwoWay}" 
         SelectedItem="{Binding Path=seletedItem}" 
         DataContext="{Binding}"> 
      </ComboBox> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

ComboBox對象

public class ComboboxObj 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    //public string selectedItem { get; set; } 
} 

DataGrid行對象

public class ListDataExtract 
{ 
    public SP_List_Details Detail { get; set; } 
    public List<ComboboxObj> masterData { get; set; } 
    public ComboboxObj seletedItem { get; set; } 
} 

主要工藝流程

 for (int i = 0; i < lstDetail.Count; i++) 
      { 
       ComboboxObj cbxObj = new ComboboxObj(); 
       ListDataExtract extract = new ListDataExtract(); 
       extract.Detail = lstDetail[i]; 
       extract.masterData = lstCbx; 
       // Create Seleted Item 
       cbxObj.Id = lstDetail[i].DataType_Id.Value; 
       cbxObj.Name = findIndexMasterData(lstDetail[i].DataType_Id.Value, lstCbx); 
       // lstCbx is List Object ComboboxObj 
       extract.seletedItem = lstCbx[0]; 
       // End Create Seleted Item 
       lstExtract.Add(extract); 
      } 
      DataGridListDetails.ItemsSource = lstExtract; 
+0

你的類必須實現INotifyPropertyChanged模式。你可以在網上找到很多文檔。 http://stackoverflow.com/questions/3505716/how-to-use-inotifypropertychanged-correctly-in-wpf-xaml –

+0

謝謝,但我可以寫函數RaisePropertyChanged :( – jonny

+0

我的新問題是「無法評估表達式,因爲當前線程處於堆棧溢出狀態「:( – jonny

回答

0

對不起,夥計,我試圖複製你的代碼,並找出,使其工作,但它只是沒有任何意義,好像你仍然有很多東西需要學習。如果你想學習WPF,你需要知道什麼是DataBinding

我的建議是:從小開始,讓它成長。首先:顯示一個簡單的包含字符串的組合框,從代碼後面填充(xaml.cs)。喜歡的東西:

<Grid> 
    <ComboBox x:Name="MyCombo"></ComboBox> 
</Grid> 

而且在後面

var myComboItemsSource = new List<string>(); 
    MyCombo.ItemsSource = myComboItemsSource; 

    myComboItemsSource.Add("hello"); 
    myComboItemsSource.Add("world"); 

的代碼,然後用數據綁定做到這一點:

<ComboBox ItemsSource="{Binding MyComboItemsSource}" SelectedItem="{Binding SelectedItem}"></ComboBox> 

只需從代碼隱藏注入的DataContext:

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = new ViewModel(); 
} 

而且建立視圖模型:

public abstract class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var propertyChanged = PropertyChanged; 

     propertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
public class ViewModel : ViewModelBase 
{ 
    private string _selectedItem; 
    public List<string> MyComboItemsSource { get; } 

    public string SelectedItem 
    { 
     get { return _selectedItem; } 
     set 
     { 
      _selectedItem = value; 
      NotifyPropertyChanged(); 
     } 
    } 

    public ViewModel() 
    { 
     MyComboItemsSource = new List<string>(); 
     MyComboItemsSource.Add("hello"); 
     MyComboItemsSource.Add("world"); 

     SelectedItem = MyComboItemsSource.First(); 
    } 
} 

(注INotifyPropertyChanged的的示例實現)

之後,它應該很容易建立它在DataGrid中,如果你想。

只是讓你的步驟更小。

希望它有幫助。

相關問題