2015-07-11 56 views
0

在我的項目中,我有一個包含三個組合框模板列的數據網格,並且數據綁定到三個集合,並且這些列是可編輯的。第二個和第三個組合框中的集合根據第一列中組合框的選定索引進行選擇,當我更改任何行中第一列中的選擇時,更改將反映在所有行中。這裏是我的xaml代碼wpf模板組合框中的選擇更改列更改其他行中的值

<DataGrid x:Name="dtg" 
       Grid.Row="2" 
       AutoGenerateColumns="False" 
       CanUserAddRows="True" 
       IsReadOnly="False" 
       VerticalScrollBarVisibility="Visible" 
       EnableRowVirtualization="False" 
       SelectionUnit="CellOrRowHeader" 
       SelectionMode="Extended" 
       ItemsSource="{Binding MainDataCollection, Mode= TwoWay}" 
       AlternatingRowBackground="{DynamicResource AccentColorBrush2 }" 
       GridLinesVisibility="Horizontal" 
       KeyUp="Dtg_OnKeyUp" > 
     <DataGrid.Columns> 
      <DataGridTextColumn x:Name="slnoColunColumn" 
           Header="slno." 
           IsReadOnly="True" 
           Width="75" 
           Binding="{Binding Mode=OneWay , Path = Slno}"></DataGridTextColumn> 

      <DataGridTemplateColumn Header="Category" Width="*" x:Name="categoryColumn"> 


       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox x:Name="categoryBox" 
          IsEditable="True" 
          controls:TextBoxHelper.ClearTextButton="True" 
          controls:TextBoxHelper.SelectAllOnFocus="True" 
          controls:TextBoxHelper.Watermark="Category" 
          MaxDropDownHeight="125" 
          SelectionChanged="CategoryBox_OnSelectionChanged" 
          DisplayMemberPath="CategoryName" 
          SelectedValuePath="CategoryId" 
          ItemsSource="{Binding Path=DataContext.CategoriesCollection, 
          RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

      <DataGridTemplateColumn Header="Question" Width="*" x:Name="questionColumn"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox x:Name="questionBox" 
          IsEditable="True" 
          controls:TextBoxHelper.ClearTextButton="True" 
          controls:TextBoxHelper.SelectAllOnFocus="True" 
          controls:TextBoxHelper.Watermark="Question" 
          MaxDropDownHeight="125" 
          DisplayMemberPath="TheQuestion" 
          SelectedValuePath="QuestionID" 
          ItemsSource="{Binding Path = DataContext.QuestionsCollection, 
          RelativeSource = {RelativeSource FindAncestor, AncestorType = DataGrid}}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

      <DataGridTemplateColumn Header="Answer" Width="*" x:Name="AnswerColumn"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox x:Name="answerBox" 
          IsEditable="True" 
          controls:TextBoxHelper.ClearTextButton="True" 
          controls:TextBoxHelper.SelectAllOnFocus="True" 
          controls:TextBoxHelper.Watermark="Answer" 
          MaxDropDownHeight="125" 
          DisplayMemberPath="TheAnswer" 
          SelectedValuePath="AnswerID" 
          ItemsSource="{Binding Path = DataContext.AnswersCollection, 
          RelativeSource = {RelativeSource FindAncestor, AncestorType= DataGrid}}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

     </DataGrid.Columns> 
    </DataGrid> 

有人知道爲什麼它的行爲是這樣的。任何人都可以找到一個解決方案來解決這個

方法添加數據

public void AddData() 
    { 
     if (MainDataCollection== null) MainDataCollection = new ObservableCollection<GridDataSource>(); 
     if (CategoriesCollection == null) CategoriesCollection = new ObservableCollection<Category>(); 
     if(QuestionsCollection == null) QuestionsCollection = new ObservableCollection<Question>(); 
     if(AnswersCollection == null) AnswersCollection = new ObservableCollection<Answer>(); 

     AddDataGridRow(0); 
     var data = _dataProvider.GetCategoriesTable(0); 

     foreach (DataRow row in data.Rows) 
     { 

       CategoriesCollection.Add(new Category(int.Parse(row[0].ToString()),row[1].ToString())); 
     } 

    } 

方法來添加新的數據網格行

public void AddDataGridRow(int slno) 
    { 
     MainDataCollection.Add(new GridDataSource(slno+1,"","","")); 
    } 

方法得到的問題

public void GetQuestions(int categoryId) 
    { 
     if (QuestionsCollection == null)QuestionsCollection = new ObservableCollection<Question>(); 
     QuestionsCollection.Clear(); 
     var data = _dataProvider.GetQuestionsTable(categoryId); 
     foreach (DataRow row in data.Rows) 
     { 
      QuestionsCollection.Add(new Question(int.Parse(row[0].ToString()),row[1].ToString())); 
     } 

     GetAnswers(categoryId);    
    } 

方法獲得答案

public void GetAnswers(int categoryId) 
    { 
     if (AnswersCollection == null) AnswersCollection = new ObservableCollection<Answer>(); 
     AnswersCollection.Clear(); 
     var data = _dataProvider.GetAnswersTable(categoryId); 
     foreach (DataRow row in data.Rows) 
     { 
      AnswersCollection.Add(new Answer(int.Parse(row[0].ToString()), row[1].ToString())); 
     } 

    } 

組合框中選擇改變

private void CategoryBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ComboBox cb = sender as ComboBox; 
     _mainWindowViewModel.GetQuestions((int)cb.SelectedValue);    
    } 

回答

1

嘗試增加這所有3個組合框:

IsSynchronizedWithCurrentItem="False"

我猜這已經解決了這個網站:)

+0

前它不起作用 – Sony

+0

你能發表一些ViewModel代碼嗎? – slimbofat

+0

代碼來填充這些組合框? – Sony

相關問題