2016-11-11 74 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中,如果你想。

只是让你的步骤更小。

希望它有帮助。

相关问题