【问题标题】:WPF how to use properties to redraw another element?WPF如何使用属性重绘另一个元素?
【发布时间】:2013-10-08 07:52:33
【问题描述】:

所以我有一个名为信号图的类,我想在滚动查看器执行任务时对其进行更新。

信号图通过这些依赖属性连接到滚动查看器:

 public double MinimumXInDIPs
    {
      get { return (double)GetValue(MinimumXInDIPsProperty); }
      set
      {
        SetValue(MinimumXInDIPsProperty, value);
      }
    }
    public static readonly DependencyProperty MinimumXInDIPsProperty =
      DependencyProperty.Register("MinimumXInDIPs",
      typeof(double), typeof(SignalGraph),
      new FrameworkPropertyMetadata(new double()));

    public double ViewportWidth
    {
      get { return (double)GetValue(ViewportWidthProperty); }
      set
      {
        SetValue(ViewportWidthProperty, value);
      }
    }
    public static readonly DependencyProperty ViewportWidthProperty =
      DependencyProperty.Register("ViewportWidth",
      typeof(double), typeof(SignalGraph),
      new FrameworkPropertyMetadata(new double()));


    public int UnitsOfTimePerAxisDivision
    {
      get { return (int)GetValue(UnitsOfTimePerAxisDivisionProperty); }
      set
      {
        SetValue(UnitsOfTimePerAxisDivisionProperty, value);
      }
    }
    public static readonly DependencyProperty UnitsOfTimePerAxisDivisionProperty =
      DependencyProperty.Register("UnitsOfTimePerAxisDivision",
      typeof(int), typeof(SignalGraph),
      new FrameworkPropertyMetadata(1));

但是,我不想只对三个属性中的每一个都使用依赖属性回调,因为所有这三个属性都可能在缩放时发生变化,这会导致重绘信号图的次数过多。

通常我认为我只会将所有信号图注册到 ZoomEvent 上,但信号图无法直接访问滚动查看器。我认为在 WPF 中以某种方式使用属性连接它们会更自然。

我可以在滚动查看器上创建一个属性 bool NeedsToRedraw,我只在缩放发生时才将其设置为 true。然后我可以将信号图绑定到该属性,并拥有一个调用重绘函数的 propertychangedcallback。但是,如果我将 bool 重置为 false,它最终会再次调用 propertychangedcallback 一次,即使我真的不需要调用该函数。

这会效率低下吗?我觉得在某些方面事件会更干净,因为当我将值设置为 true(导致重绘)时,我不会先调用 propertychangedcallback,然后再将值设置回 false(导致回调中没有代码,只是一个无用的测试)>

我想这没什么大不了的,但我只是想知道人们会推荐什么。

我想更新一次

【问题讨论】:

    标签: c# wpf xaml binding dependency-properties


    【解决方案1】:

    使用“NeedsToRedraw”似乎可以正常工作,并且多余的属性更改回调不太可能导致性能瓶颈。但是,也许其中一个是更好的选择:

    1. 由于这三个属性是相互关联的,因此请将它们合并为一个,在 ZoomProperties 之类的包装类中。 (当然,这也需要更改消费者类。)
    2. 在目标上添加一个计时器。当属性更改时启动/重置它,并且仅在某个增量(可能是 50 毫秒?)过去时触发处理程序。

    【讨论】:

      猜你喜欢
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2012-02-13
      • 2018-10-18
      • 1970-01-01
      相关资源
      最近更新 更多