2015-01-10 52 views
1

我想知道如何做到這一點。 我有一個DataGridTemplateColumn與簡單的ComboBox控制裏面。 組合框有一個鏈接到它的SelectionChanged事件。WPF - 獲取DataGridTemplateColumn中Combobox控件的行索引

在已更改的事件中,我想知道已更改的行的行索引是從已更改的組合框派生出來的。

我是否採取了錯誤的做法? 這是我有:

<DataGrid AutoGenerateColumns="False" Margin="5,10,5,5" 
      x:Name="dgrMatches" ItemsSource="{Binding .}" 
      CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" 
      SelectionMode="Single" SelectionUnit="FullRow" IsReadOnly="False" 
      RowStyle="{DynamicResource EditableRows}" CellStyle="{DynamicResource EditableTableCells}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn ... /> 

      <DataGridTemplateColumn Header="Legs won" Width="Auto"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox Name="cbbLegsWonA" 
           SelectedIndex="{Binding LegsWonA, Mode=TwoWay}" 
           ItemsSource="{Binding NumberOfLegs}" 
           SelectionChanged="cbbLegsWonA_SelectionChanged" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

      <!-- @Chris Eelmaa --> 
      <DataGridTemplateColumn Header="Legs won" Width="Auto"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox Name="cbbLegsWonB" 
           SelectedIndex="{Binding LegsWonB, Mode=TwoWay}" 
           ItemsSource="{Binding NumberOfLegs}" 
           SelectionChanged="cbbLegsWonB_SelectionChanged" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

      <DataGridTextColumn ... /> 
     </DataGrid.Columns> 
    </DataGrid> 

且事件處理程序:

private void cbbLegsWonA_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ComboBox cbbLegsA = e.Source as ComboBox; // Altered combobox 
    int rowIndex = -1; 

    if (cbbLegsA.Tag == null) 
    { 
     DataGridRow row = (DataGridRow)dgrMatches.ContainerFromElement(cbbLegsA); 
     rowIndex = row.GetIndex(); 
     cbbLegsA.Tag = rowIndex; 
    } 
    else 
    { 
     Int32.TryParse(cbbLegsA.Tag.ToString(), out rowIndex); 
    } 

//@ChrisEelmaa: Basically, change the bound list and refresh the items in the datagrid 
//The debugger doesn't get to this point, ofcourse 
SingleMatch match = matches.ElementAt(rowIndex); // Get the current match out of the bound list 
match.LegsWonA = cbbLegsA.SelectedIndex; // Manually change second combobox item 
dgrMatches.Items.Refresh(); 

... 
} 

這不起作用:(DataGridRow)dgrMatches.ContainerFromElement(cbbLegsA) == null

+1

取決於你如何定義「wroong方法」。這當然不是一種與WPF緊密相關的MVVM方法。重構所有使用MVVM模式的內容,並解釋爲什麼您首先需要這些信息,然後我可以提供一些提示。 –

+0

我省略了一些細節,這是真的。我不太確定MVVM的真正含義。但是我有兩個'DataGridTemplateColumn',它的組合框與問題中的類似。當第一個組合框發生變化時,我想更改第二個組合框的值,反之亦然。這就是爲什麼我需要行索引,所以我可以通過直接更改數據網格中的值來更改第二個組合框。 – DerpyNerd

+0

@ChrisEelmaa我更新了datagrid給你一個想法 – DerpyNerd

回答

1

(DataGridRow)dgrMatches.ContainerFromElement(cbbLegsA) == null不起作用,因爲DataGrid的ItemContainer爲特定的行而不是DataTemplate中的ComboBox,它是一個DataGridRow,它包含ComboBox的「模板化」版本。相反,您需要使用VisualTreeHelper .FindParent()從ComboBox中查找DataGridRow(因爲ComboBox位於DataGridRow的可視化樹中,但不在邏輯樹中)。您可以從DataGridRow引用輕鬆找到行索引。然而...

評論中提出的一種更好的方法是使用MVVM模式。您的ComboBox將被綁定到ViewModel中的屬性。當ViewModel中的一個屬性發生變化時,您可以輕鬆地更新另一個屬性,以實現所需的行爲,而不會通過可視化樹進行任何難看的搜索,或者背後有一堆UI代碼。而不是一個ComboBox自動更新UI中的另一個ComboBox的邏輯,它更容易控制視圖的對象模型(又稱'ViewModel'),它應該在哪裏。

+0

我一直在研究MVVM,它實際上是我在我的應用程序(類型)中所做的,但有一些我沒有應用的關鍵元素。我現在已經建立了我的模型,視圖,視圖模型和視圖結構。正如你所建議的,它沒有任何令人討厭的樹遍歷!非常感謝 – DerpyNerd

相關問題