来自资源字典 GetTemplateChild 的 wpf viewport3d 返回 null

本文关键字:wpf viewport3d 返回 null GetTemplateChild 资源 字典 | 更新日期: 2023-09-27 18:34:03

>我已将所有模型添加为资源字典作为控件模板,我可以使用此代码加载它

 <Control Name="control" Width="507" Height="309" Template="{StaticResource SkeletonModel}"

现在我想在运行时编辑该模型,所以我试图让加载到控制元素中的 viewPort3d 在运行时对其进行编辑,但我不能为它使用触发器,所以我必须引用它然后从后面的代码中操作它是我尝试的代码

  public partial class MainWindow : Window
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Viewport3D v = this.GetTemplateChild("ModelView") as Viewport3D;
        }
        public MainWindow()
        {
            InitializeComponent();
        }
    } 

v 总是 = null 我也尝试将静态资源更改为动态资源它不起作用,我还尝试了其他几种方法,例如

Viewport3D v = control.Template.LoadContent() as Viewport3D;
            ModelVisual3D model;
            model = FindName("SkeletonHumanModel") as ModelVisual3D;

这确实加载了视口和模型,但是当我编辑它们中的任何一个时,它都不会被编辑,所以我想 loadContent 只是加载副本或其他实例,而不是窗口中使用的当前实例这是资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                    mc:Ignorable="d"
    >
    <!--x:Class="Dynamic.ModelOperations"
                     x:ClassModifier="public"-->
    <ControlTemplate x:Key="SkeletonModel" >
        <Viewport3D Width="500" Height="500"  x:Name="ModelView"  ClipToBounds="True" >
            <Viewport3D.Camera>
                <PerspectiveCamera FieldOfView="35" FarPlaneDistance="110.57139405398024" LookDirection="0,0,-38.5951803220261" NearPlaneDistance="0.1" Position="-16.3927650451661,9.1631622056011,38.1892213715065" UpDirection="0,1,0"/>
            </Viewport3D.Camera>
            <ModelVisual3D x:Name="World">
                <ModelVisual3D x:Name="AmbientLightContainer">
                    <ModelVisual3D.Content>
                        <AmbientLight x:Name="AmbientLight" Color="#FF7F7F7F"/>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
                <ModelVisual3D x:Name="DirectionalLightContainer">
                    <ModelVisual3D.Content>
                        <DirectionalLight x:Name="DirectionalLight" Color="#FF3F3F3F" Direction="0,0,-1">
                            <DirectionalLight.Transform>
                                <TranslateTransform3D OffsetZ="3" OffsetX="0" OffsetY="0"/>
                            </DirectionalLight.Transform>
                        </DirectionalLight>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
                <ModelVisual3D x:Name="SkeletonHumanModel"   >.....

提前致谢

来自资源字典 GetTemplateChild 的 wpf viewport3d 返回 null

要从 ResourceDictionary 获取资源,您应该使用 FindResource() 方法。如果你的资源字典合并到应用资源中,你可以从主窗口获取这样的资源,

private void OnLoaded(object sender, RoutedEventArgs e)
{
    var item = this.FindResource("SkeletonModel") as Viewport3D;
}