2

我正在构建一个具有 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, 0);
      var point1 = new Point3D(0, theData.Y, 0);
      var point2 = new Point3D(0, 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 p1, 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 中的事件处理程序为空,所以我想我缺少一些绑定机制。我尝试了很多不同的方法。我已经尝试阅读有关 WPF 中的数据绑定和有关 DataContext 的信息,但似乎无法弄清楚它是如何工作的或如何使用它。我还阅读了有关依赖属性的信息,但不确定这是我需要的。

(也感谢任何有关制定此最佳实践的指导。我是否应该尝试 MVVM:ify 用户控制,在这种情况下如何?或者您是否认为这是不必要的复杂性?)

这些都没有帮助我让它工作:

WPF - 简单用户控件中 的绑定 WPF 简单绑定到对象属性 子元素使用的自定义 UserControl 属性 如何将用户控件的属性绑定到 WPF 中同一控件的属性? WPF用户控件绑定问题

谢谢 /Ulf – 目前是 WPF 新手... :-)

4

0 回答 0