【问题标题】:Caliburn Micro not updating dependency property in User ControlCaliburn Micro 未更新用户控件中的依赖项属性
【发布时间】:2015-05-16 19:35:01
【问题描述】:

我有一个 Caliburn Micro View/ViewModel 集,它使用一个用户控件,该控件的一个依赖属性绑定到主视图的 ViewModel 中的一个值。当 ViewModel 中的值发生更改时,我似乎无法通知用户控件。

视图模型:

OutputImage.AddDirtyRect(new Int32Rect(0, 0, width, height));
OutputImage.Unlock();

NotifyOfPropertyChange(() => OutputImage);

主视图:

<local:HistogramControl x:Name="Histogram" Grid.Row="1" Grid.Column="0" OutputImage="{Binding Path=OutputImage, Mode=TwoWay}"/>

用户控制:

    public static readonly DependencyProperty OutputImageProperty =
    DependencyProperty.Register("OutputImage", typeof(BitmapSource), typeof(HistogramControl), new UIPropertyMetadata(
            new WriteableBitmap(1, 1, 96, 96, PixelFormats.Rgb24, null),
            new PropertyChangedCallback((s, e) =>
            {
                var source = s as HistogramControl;
                source.UpdateHistogram();
            })));
    public BitmapSource OutputImage
    {
        get { return (BitmapSource)GetValue(OutputImageProperty); }
        set { SetValue(OutputImageProperty, value); }
    }

如果我在PropertyChangedCallback(...) lambda 中的用户控件代码中放置一个断点,它将在应用程序启动时命中一次,并提供 ViewModel 类的构造函数中设置的初始 OutputImage,但它不会被调用再次调用上面显示的 ViewModel 代码并且 OutputImage 发生变化。

【问题讨论】:

    标签: c# wpf mvvm dependency-properties caliburn.micro


    【解决方案1】:

    问题在于,WriteableBitmap 是通过指向其内存的指针在不安全的代码中修改的,因为图像数据被复制到像素缓冲区中。这并没有触发绑定更新,因为绑定的对象并没有真正更新。由于绑定是通过引用用户控件内的绑定值中的像素数据在更改时更新的,因此没有通过调用PropertyChangedCallback(...) 获得通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多