访问 c# 中 contentpresenter 中的控件

编程入门 行业动态 更新时间:2024-10-26 04:30:54
本文介绍了访问 c# 中 contentpresenter 中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何访问位于 contentpresenter 的内容模板中的命名控件.如何从 cs 文件访问 webview 控件(x:name=detView).

How to access a named control that is in the content template of the contentpresenter. how to access the webview control(x:name=detView) from cs file.

        <ContentPresenter
            x:Name="DetailContentPresenter"
            Grid.Row="0"
            BorderBrush="{ThemeResource SystemControlForegroundBaseLowBrush}"
            Content="{x:Bind coll.SelectedItem,Mode=OneWay}">
            <ContentPresenter.ContentTemplate>
                <DataTemplate x:DataType="data:coll_Details" x:Name="ttt">
                    <Grid>
                           <WebView DefaultBackgroundColor="#F5F5F5" x:Name="detView" Source="ms-appx-web:///Assets/Web/collDetails.html"/>
                    </Grid>
                </DataTemplate>
            </ContentPresenter.ContentTemplate>
            <ContentPresenter.ContentTransitions>
                <TransitionCollection/>
            </ContentPresenter.ContentTransitions>
        </ContentPresenter>

推荐答案

如果您像 官方文档.

您可以通过controlName.ContentTemplateRoot 获取模板.我根据上面官方文档的Example做了一个demo,在DataTemplate里面放了一个webview.

You can get the template through the controlName.ContentTemplateRoot. I made a demo from the Example of official documentation above, and put a webview inside the DataTemplate.

MainPage.xaml:

MainPage.xaml:

<Page.Resources>
    <Style TargetType="HyperlinkButton" x:Key="myStyle" >
        ...
        <Setter Property="Template" x:Name="presenterSetter">
            <Setter.Value>
                <ControlTemplate TargetType="HyperlinkButton">
                    <Grid x:Name="rootGrid">
                        ...
                        <Border x:Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Margin="3">
                            <ContentPresenter x:Name="MyContentPresenter"
                                          Content="{TemplateBinding Content}"
                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                        >
                                <ContentPresenter.ContentTemplate>
                                    <DataTemplate  x:Name="ttt">
                                        <Grid>
                                            <WebView Source="ms-appx-web:///Assets/Web/default.html" Name="myWebView"/>
                                        </Grid>
                                    </DataTemplate>
                                </ContentPresenter.ContentTemplate>
                            </ContentPresenter>
                        </Border>
                        <!--focus visuals omitted-->
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel VerticalAlignment="Bottom">
        <HyperlinkButton Name="myHyperlink" Style="{StaticResource myStyle}">This is a link</HyperlinkButton>
        <Button Click="Button_Click" Name="myBtn">Click Me</Button>
    </StackPanel>
</Grid>

我可以使用以下代码获取 WebView:

And I can get the WebView using the codes below:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var myView= ((Grid)myHyperlink.ContentTemplateRoot).Children[0] as WebView;
}

这篇关于访问 c# 中 contentpresenter 中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 09:16:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   contentpresenter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!