问题描述
|
我正在构建一个具有ViewPort3D的用户控件。我希望能够使用ModelVisual3D的绑定属性更新视图端口(通过一种接受方法,该方法接受用于创建可视模型的数据)。为了尝试用户控件,我也尝试在窗口中使用它。
我相信我在绑定方面缺少一些东西,想知道是否有人可以通过指出我所缺少的东西来帮助我。
这是我的代码:
用户控件
<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>
后面的代码
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.ComponentModel;
namespace TheProject
{
public partial class TheUserControl : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this,new PropertyChangedEventArgs(name));
}
private ModelVisual3D _model;
protected ModelVisual3D Model
{
get { return this._model; }
set
{
this._model = value;
OnPropertyChanged(\"Model\");
}
}
public void SetTheData(ITheData theData)
{
if (theData != null)
{
this.Model = SimpleTriangleModel(theData);
}
}
private static ModelVisual3D SimpleTriangleModel(ITheData theData)
{
var point0 = new Point3D(theData.X,0);
var point1 = new Point3D(0,theData.Y,0);
var point2 = new Point3D(0,theData.Z);
return new ModelVisual3D
{
Content = CreateTriangleModel(point0,point1,point2,new SolidColorBrush(Colors.Tomato))
};
}
public static Model3DGroup CreateTriangleModel(Point3D p0,Point3D p1,Point3D p2,Brush brush)
{
var mesh = new MeshGeometry3D();
mesh.Positions.Add(p0);
mesh.Positions.Add(p1);
mesh.Positions.Add(p2);
mesh.TriangleIndices.Add(0);
mesh.TriangleIndices.Add(1);
mesh.TriangleIndices.Add(2);
Vector3D normal = Calculatenormal(p0,p1,p2);
mesh.normals.Add(normal);
mesh.normals.Add(normal);
mesh.normals.Add(normal);
Material material = new DiffuseMaterial(brush);
var model = new GeometryModel3D(mesh,material);
var group = new Model3DGroup();
group.Children.Add(model);
return group;
}
public static Vector3D Calculatenormal(Point3D p0,Point3D p2)
{
var v0 = new Vector3D(p1.X - p0.X,p1.Y - p0.Y,p1.Z - p0.Z);
var v1 = new Vector3D(p2.X - p1.X,p2.Y - p1.Y,p2.Z - p1.Z);
return Vector3D.Crossproduct(v0,v1);
}
}
}
数据接口
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>
后面的代码
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;
}
}
}
OnPropertyChanged中的事件处理程序为null,因此我想我缺少一些绑定机制。我尝试了很多不同的方法。
我已经尝试阅读有关WPF中的数据绑定和DataContext的内容,但似乎无法弄清楚它是如何工作或如何使用的。我还阅读了有关依赖项属性的信息,但不确定那是我所需要的。
(此外,对于实现此最佳实践的任何指导,我们也表示赞赏。我应该尝试使用MVVM:ify用户控件,在这种情况下,如何做?还是您认为这是不必要的复杂性?)
这些都没有帮助我工作:
WPF-简单用户控件中的绑定
WPF简单绑定到对象属性
子元素使用的自定义UserControl属性
如何在WPF中将用户控件的属性绑定到相同控件的属性?
WPF用户控件绑定问题
谢谢
/ Ulf –当前是WPF新手... :-)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)