2013-08-01 54 views
0

我試圖在DataGrid中的DataTemplate中查找ComboBox。從指定的DataGridRow中查找DataGrid中的ComboBox

首先,我試試吧:

VisualTreeHelper.GetChildrenCount(dataGridRowInstance); 

但它總是返回0

XAML:

   <DataGrid HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" Height="231" Width="571" ItemsSource="{Binding}" AutoGenerateColumns="false" x:Name="gridRestore" CellStyle="{StaticResource CellVerticalAlignCenter}"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="Created Date" Binding="{Binding Path=CreationTime, ConverterCulture=pt-BR}" Width="120"/> 
         <DataGridTextColumn Header="Length" Binding="{Binding Path=Length}" Width="120"/> 
         <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="200"/> 
         <DataGridTemplateColumn Width="*" Header="Restore To..."> 
          <DataGridTemplateColumn.CellTemplate> 
           <DataTemplate> 
            <ComboBox Name="cbx" Width="120" DropDownOpened="Cbx_OnDropDownOpened" SelectionChanged="Cbx_OnSelectionChanged"></ComboBox> 
           </DataTemplate> 
          </DataGridTemplateColumn.CellTemplate> 
         </DataGridTemplateColumn> 
        </DataGrid.Columns> 
       </DataGrid> 

在恢復,我想根據測試結果隱藏組合框我在DataGrid.LoadingRow事件中做。

+0

你可以發佈DataGrid.LoadingRow事件嗎? – Shoe

+0

什麼是dataGridRowInstance?請發佈更多代碼。 –

回答

0

找到放入DataTemplate中的元素並不容易。你可以在這裏找到一個例子http://msdn.microsoft.com/en-us/library/bb613579.aspx

假設您使用的是MVVM模式,並且您在DataGrid的每行創建了一個虛擬機,我建議爲您的虛擬機添加一個新屬性並將組合框的可見性綁定到它。