如何访问datagrid ColumnHeaderStyle中的控件?

时间:2012-07-15 05:05:45

标签: wpf styles wpfdatagrid

如何访问datagrid ColumnHeaderStyle中的控件?

在此示例中:

   <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid Name="grdPeresenter" VerticalAlignment="Center" HorizontalAlignment="Stretch">
                       <TextBox x:Name="txtSearch" Grid.Row="1" BorderThickness="2"  PreviewMouseLeftButtonUp="grid1_PreviewMouseLeftButtonUp"  HorizontalAlignment="Stretch" TextChanged="TextBox_TextChanged" >
                           </TextBox>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataGrid x:Name="grd" ItemsSource="{Binding Source={StaticResource theSource}}" AutoGenerateColumns="False"  
              ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"  PreviewKeyDown="grd_PreviewKeyDown"
              SelectedIndex="{Binding SelectedIndex}"
              SelectedItem="{Binding SelectedItem}"
              behavior:MouseDoubleClick.Command="{Binding MouseDoubleClickCommand}" 
              GridLinesVisibility="Vertical">
    </DataGrid>

我想访问例如datagrid的第一列的textSearch。它可能吗?怎么样?

1 个答案:

答案 0 :(得分:1)

根据this answer

 var res = FindVisualChildren<TextBox>(grd).Where(t => t.Name == "txtSearch");

这里你可以找到所有“txtSearch”标题textBox。

我确信它可以更直接的方式,但在那之前......