查找ListView Wpf中的控件

时间:2016-05-16 06:12:59

标签: c# wpf gridview datagrid

 <ListView Height="Auto"   Name="lstIndent" SelectionMode="Single" Grid.ColumnSpan="5"  HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0"   >
    <ListView.View >
       <GridView x:Name="dgIndentDetails" >
          <GridViewColumn Width="Auto"   >
             <GridViewColumn.CellTemplate>
                <DataTemplate>
                   <!--<GroupBox Header="Department Issue Header" Grid.Row="5" Grid.ColumnSpan="5"  HorizontalAlignment="Stretch"  >-->
                      <Grid>
                         <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                         </Grid.ColumnDefinitions>
                         <Grid.RowDefinitions>
                            <RowDefinition  Height="Auto"  />
                            <RowDefinition  Height="Auto"  />
                         </Grid.RowDefinitions>
                         <CheckBox Grid.Row="0" Grid.Column="0" IsChecked="{Binding Path=IsIndentIssue, UpdateSourceTrigger=PropertyChanged}" x:Name="chkbxIsChecked" HorizontalAlignment="Center"/>
                         <Label Grid.Column="1" Grid.Row="0" Content="{Binding ItemName}" x:Name="lbllstItemName" HorizontalAlignment="Center" ></Label>
                         <Label Grid.Column="2"  Grid.Row="0" Content="{Binding Quantity}" x:Name="lbllstQty" HorizontalAlignment="Center" ></Label>
                         <Label Grid.Column="3"  Grid.Row="0" Content="{Binding IssueQuantity}" x:Name="lbllstIssuedQty" HorizontalAlignment="Center" ></Label>
                         <DataGrid  Height="Auto" Padding="10,10,10,10" Width="800" Grid.Row="1" Grid.ColumnSpan="5" Name="dgIssuedItemsBatchDetails"  AutoGenerateColumns="False"  HorizontalAlignment="Stretch" VerticalAlignment="Center">
                            <DataGrid.Columns>                                                                 
                               <DataGridTextColumn Binding="{Binding ItemName}">                                                                     
                                  <DataGridTextColumn.Header>                                                                         
                                     <TextBlock Text="ItemName"/>                                                                    
                                  </DataGridTextColumn.Header>                                                                
                               </DataGridTextColumn>                                                                 
                               <DataGridTextColumn Binding="{Binding BatchNo}">
                                   <DataGridTextColumn.Header>
                                      <TextBlock Text="BatchNo"/>
                                   </DataGridTextColumn.Header>       
                               </DataGridTextColumn>                                                                 
                               <DataGridTextColumn Binding="{Binding Quantity}">
                                  <DataGridTextColumn.Header>                                                                         
                                    <TextBlock Text="Quantity"/>                                                                    
                                  </DataGridTextColumn.Header>
                               </DataGridTextColumn>
                           </DataGrid.Columns>
                          </DataGrid>
                       </Grid>
                    <!--</GroupBox>-->
                 </DataTemplate>
              </GridViewColumn.CellTemplate>
          </GridViewColumn>
       </GridView>
    </ListView.View>
 </ListView>

我需要找到DataGrid dgIssuedItemsBatchDetails然后绑定。 DataGrid位于GridView内,而GridView位于ListView内。

GridView grdvwIssueDetail = (GridView)lstIndent.FindName("dgIndentDetails");

通过使用此代码,我可以找到GridView。我的DataGrid放在GridView DataTemplate中。建议我如何从代码中找到我的DataGrid

1 个答案:

答案 0 :(得分:2)

可以使用VisualTreeHelper来实现。 正如MSDN所说:

  

VisualTreeHelper类提供了实用程序方法,这些方法执行涉及可视树中节点的常见任务。

因此,让我举例说明如何实现它:

    private void SeeTheChild()
    {
        DataGrid myCombo = GetVisualChildInDataTemplate<DataGrid>(lstIndent);            
    }
    private T GetVisualChildInDataTemplate<T>(DependencyObject parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChildInDataTemplate<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

VisualTreeHelper.GetChildrenCount(parent)返回0,因为ListView已创建,但尚未加载。特别是DataTemplate尚未应用于ListView,因此视觉树中没有任何内容。
然后使用此方法获取DataGrid的{​​{1}} Loaded事件:

Window

<强>更新

private void Window_Loaded(object sender, RoutedEventArgs e) { SeeTheChild(); } 中找到每个项目的控件:

创建ListView事件:

SelectionChanged

以及可以为您找到必要控制的方法:

private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ItemContainerGenerator generator = this.listView.ItemContainerGenerator;
   ListBoxItem selectedItem = (ListBoxItem)generator.ContainerFromIndex(listView.SelectedIndex);
   Label aLabel = GetChildrenByType(selectedItem, typeof(Label), "label") as Label;
   if (aLabel != null)
   {
      MessageBox.Show("We've found Label with name 'label': " + aLabel.Content);
   }
   else
   {
      MessageBox.Show("There is no such Label");
   }
}