WPF ViewPort3D用户控件与ModelVisual3D属性绑定?
我正在构建一个具有ViewPort3D的用户控件。我希望能够使用ModelVisual3D的绑定属性更新视图端口(通过一个接受用于创建可视化模型的数据的公开方法)。为了尝试用户控件,我还尝试在窗口中使用它。 我相信我遗漏了一些关于装订的东西,不知道是否有人能帮我指出我遗漏了什么 这是我的代码: 用户控件WPF ViewPort3D用户控件与ModelVisual3D属性绑定?,wpf,user-controls,binding,viewport3d,modelvisual3d,Wpf,User Controls,Binding,Viewport3d,Modelvisual3d,我正在构建一个具有ViewPort3D的用户控件。我希望能够使用ModelVisual3D的绑定属性更新视图端口(通过一个接受用于创建可视化模型的数据的公开方法)。为了尝试用户控件,我还尝试在窗口中使用它。 我相信我遗漏了一些关于装订的东西,不知道是否有人能帮我指出我遗漏了什么 这是我的代码: 用户控件 <UserControl x:Class="TheProject.TheUserControl" xmlns="http://schemas.microsoft.
<UserControl x:Class="TheProject.TheUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="ThisUserControl"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Viewport3D Name="mainViewPort" ClipToBounds="True">
<Viewport3D.Camera>
<PerspectiveCamera
FarPlaneDistance="100"
LookDirection="-11,-10,-9"
UpDirection="0,1,0"
NearPlaneDistance="2"
Position="11,10,9"
FieldOfView="70" />
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D Content="{Binding Path=Model, ElementName=ThisUserControl}" />
<ModelVisual3D>
<ModelVisual3D.Content>
<DirectionalLight
Color="White"
Direction="-2,-3,-1">
</DirectionalLight>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D.Children>
</Viewport3D>
</Grid>
</UserControl>
数据接口
namespace TheProject
{
public interface ITheData
{
double X { set; get; }
double Y { set; get; }
double Z { set; get; }
}
}
使用窗口的用户控件
<Window x:Class="TheProject.TheWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TheProject"
Title="The Window" Height="416" Width="649">
<Grid>
<local:TheUserControl x:Name="TheUserControl1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>
</Window>
OnPropertyChanged中的事件处理程序为null,因此我认为缺少一些绑定机制。我试过很多不同的方法。
我试过阅读WPF中的数据绑定和DataContext,但似乎不知道它是如何工作的,也不知道如何使用它。我也读过关于依赖属性的内容,但不确定这是我需要的
(也有任何对这一最佳实践的指导值得赞赏。我应该尝试MVVM:将用户控件化,在这种情况下如何?或者您认为这是不必要的复杂性?)
这些都没有帮助我让它工作: 谢谢/Ulf–目前是WPF新手…:-) 我意识到我不需要将模型绑定到属性,但可以使用SetTheData方法处理/向主视口成员添加相关的ModelVisual3D对象,因此我解决了我的问题。我还意识到ModelVisual3D(我正试图绑定到一个属性)是集合的一部分这一事实可能会造成问题。我很好奇如何将一个属性绑定到集合的一个成员。我意识到我不需要将模型绑定到属性,但可以使用SetTheData方法处理/将相关的ModelVisual3D对象添加到主视口成员,因此我解决了我的问题。我还意识到ModelVisual3D(我正试图绑定到一个属性)是集合的一部分这一事实可能会造成问题。我很好奇如何将一个属性绑定到集合的一个成员。但我想我能读懂这一点。
<Window x:Class="TheProject.TheWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TheProject"
Title="The Window" Height="416" Width="649">
<Grid>
<local:TheUserControl x:Name="TheUserControl1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>
</Window>
using System.Windows;
namespace TheProject
{
public partial class TheWindow : Window
{
private class DataClass : ITheData
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
}
public TheWindow()
{
InitializeComponent();
ITheData newData = new DataClass { X = 3, Y = 2, Z = 1 };
this.TheUserControl1.SetTheData(newData);
this.DataContext = this;
}
}
}