访问 c# 中 contentpresenter 中的控件 [英] Accessing a control which is in a contentpresenter in c#

查看:33
本文介绍了访问 c# 中 contentpresenter 中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问位于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆