listview的gridview视图中,获取列中模板内的button按钮(找控件内的控件)

时间:2022-08-16 15:00:12

点击“间隙”,获取“间隙”旁边隐藏的减号按钮(本图片未显示出来)

private void TextBlock_MouseDown_2(object sender, MouseButtonEventArgs e)
{
ContentPresenter contentPres = (sender as FrameworkElement).TemplatedParent as ContentPresenter;
DataTemplate dTemplate = contentPres.ContentTemplate;
Button quesDesc = (Button)dTemplate.FindName("delete", contentPres)

.......

}

xaml

<ListView Name="listview" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="5" Background="#D8D8D8">
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
            <GridViewColumn Header="类型" Width="80">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <TextBlock Text="{Binding Path=ItemType,Converter={StaticResource itemtypeConverter}}" MouseDown="TextBlock_MouseDown_2"></TextBlock>
                            <Button Name="delete" Visibility="Hidden" Content="-" Click="delete_Click_1"></Button>
                        </WrapPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
              </GridViewColumn>
....
        </GridView>
    </ListView.View>
</ListView>

;listview的gridview视图中,获取列中模板内的button按钮(找控件内的控件)