【问题标题】:HelixToolkit.WPF Additional DependencyProperty not workingHelixToolkit.WPF Additional DependencyProperty 不工作
【发布时间】:2021-04-03 04:54:21
【问题描述】:

我正在基于 ExampleBrwoser 中的 HelixToolkit (SurfacePlot) 中的示例应用程序之一创建应用程序。

SurfacePlotVisual3D 类具有三个依赖属性,我尝试通过复制/重命名现有的来添加另一个,但它不起作用。

XAML

<h:HelixViewport3D ZoomExtentsWhenLoaded="True" ShowCoordinateSystem="True">
    <local:SurfacePlotVisual3D CurrentX="{Binding CurrentX}" Points="{Binding Data}" ColorValues="{Binding ColorValues}" SurfaceBrush="{Binding SurfaceBrush}" />            
</h:HelixViewport3D>

“CurrentX”属性是我尝试添加的属性。

SurfacePlotVisual3D.cs

 public class SurfacePlotVisual3D : ModelVisual3D
    {
        public static readonly DependencyProperty CurrentXProperty =
          DependencyProperty.Register("CurrentX", typeof(double), typeof(SurfacePlotVisual3D),
                                      new UIPropertyMetadata(ModelChanged));

        public static readonly DependencyProperty PointsProperty =
            DependencyProperty.Register("Points", typeof(Point3D[,]), typeof(SurfacePlotVisual3D),
                                        new UIPropertyMetadata(null, ModelChanged));

        public static readonly DependencyProperty ColorValuesProperty =
            DependencyProperty.Register("ColorValues", typeof(double[,]), typeof(SurfacePlotVisual3D),
                                        new UIPropertyMetadata(null, ModelChanged));

        public static readonly DependencyProperty SurfaceBrushProperty =
            DependencyProperty.Register("SurfaceBrush", typeof(Brush), typeof(SurfacePlotVisual3D),
                                        new UIPropertyMetadata(null, ModelChanged));
...
...
    public Brush SurfaceBrush
        {
            get { return (Brush)GetValue(SurfaceBrushProperty); }
            set { SetValue(SurfaceBrushProperty, value); }
        }
        public double CurrentX
        {
            get { return (double)GetValue(CurrentXProperty); }
            set { SetValue(CurrentXProperty, value); }
        }

        private static void ModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SurfacePlotVisual3D)d).UpdateModel();
        }

MainWindow.cs

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.DataContext = new MainViewModel();
        }
     }

MainViewModel.cs

  public class MainViewModel : INotifyPropertyChanged
    {
       ...
       ...
        public Func<double, double, double> Function { get; set; }
        public Point3D[,] Data { get; set; }
        public double[,] ColorValues { get; set; }
   
        public double CurrentX { get; set; }
        public MainViewModel()
        {
            CurrentX = 6;
            MinX = 0;
            MaxX = 3;
            MinY = -2;
            MaxY = 2;
            Rows = 100;
            Columns = 50;

            Function = (x, y) => Math.Pow(Math.E, (-1) * Math.Pow(y, 2)) * 3*Math.Sin(2*x) + 2;
            ColorCoding = ColorCoding.ByGradientX;
            UpdateModel();
        }
...
...
}

我可以在 MainViewModel 中更改“Data”、“ColorValues”和“Function”,效果立即在图中可见,但 CurrentX 属性不会做任何事情。

【问题讨论】:

  • 没有实例方法UpdateModel的代码。首先需要了解的是它在VM中更新CenterX属性后调用。如果不是 - 可能您需要在注册 CurrentXProperty 时指定默认值 UIPropertyMetadata(default(double), ModelChanged))。

标签: c# wpf dependency-properties


【解决方案1】:

正如 Anton 的评论所暗示的那样,缺少默认值是问题所在。

        public static readonly DependencyProperty CurrentXProperty =
          DependencyProperty.Register("CurrentX", typeof(double), typeof(SurfacePlotVisual3D),
                                      new UIPropertyMetadata((double)0, ModelChanged));

这解决了问题。需要 (double)0 强制转换,因为没有它你会得到一个异常,因为“object”不是“double”(在我的例子中)。非常感谢安东,让我开心!

【讨论】:

    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2011-12-25
    • 2016-01-10
    • 1970-01-01
    相关资源
    最近更新 更多