[英]wpf viewport3d from resource dictionary GetTemplateChild returns null
I have added all my models as resource dictionaries as control template and i could load it using this code 我已经将所有模型作为资源字典添加为控制模板,并且可以使用此代码加载它
<Control Name="control" Width="507" Height="309" Template="{StaticResource SkeletonModel}"
now i want to edit that model in run time so iam trying to get the viewport3d that got loaded in the control element to edit it at runtime and i can't use triggers for it so i have to get reference to it then manipulate it from code behind here is the code i tried 现在我想在运行时编辑该模型,所以我试图获取已加载到控件元素中的viewport3d在运行时进行编辑,并且我无法对其使用触发器,因此我必须获取对其的引用,然后从中进行操作这里后面的代码是我尝试过的代码
public partial class MainWindow : Window
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Viewport3D v = this.GetTemplateChild("ModelView") as Viewport3D;
}
public MainWindow()
{
InitializeComponent();
}
}
v is always = null i also tried to change static resource to dynamic resource it didn't work i also tried couple of other ways like v始终= null我也尝试将静态资源更改为动态资源,但它不起作用,我还尝试了其他几种方法,例如
Viewport3D v = control.Template.LoadContent() as Viewport3D;
ModelVisual3D model;
model = FindName("SkeletonHumanModel") as ModelVisual3D;
this does loads both the viewport and the model but when i edit either of them it does not get edited so i guess that loadcontent just loads a copy or another instance not the current instance used in the window here is the resource dictionary 这确实加载了视口和模型,但是当我编辑它们中的任何一个时都不会被编辑,因此我猜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" >.....
thanks in advance 提前致谢
To get a resource from ResourceDictionary, you should use FindResource() method. 要从ResourceDictionary获取资源,应使用FindResource()方法。 If your resource dictionary is merged in App resources, from MainWindow, you can get the resource like this,
如果您将资源字典合并到App资源中,则可以从MainWindow中获取如下资源:
private void OnLoaded(object sender, RoutedEventArgs e)
{
var item = this.FindResource("SkeletonModel") as Viewport3D;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.