簡體   English   中英

WPF-依賴屬性初始化

[英]WPF - Dependency Property Initialisation

我正在嘗試實現將圖像投影到3D模型上的WPF組件。 為了使它更整潔,我嘗試使用依賴項屬性,以便可以按以下方式從View綁定到控件:

<viewers:MyViewer
    ProjectedImageSource="{Binding ViewModel.ProjectedSource}"
    Visibility="{Binding Path=HueMapVisible, Converter={StaticResource booleanToVisibilityConverter}}"
    />

控制如下:

<UserControl x:Class="ImageProjector.Controls.Viewers.MyViewer"
             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"
             d:DesignHeight="300" d:DesignWidth="300">
    <LayoutGrid>
        <Viewport3D Name="Viewport" Grid.ColumnSpan="7" Grid.RowSpan="7" />
    </LayoutGrid>
</UserControl>

代碼隱藏:

public interface IMyViewer { }

public partial class MyViewer : UserControl, IMyViewer
{
    private IImageModel3D imageModel3D;

    public MyViewer() : this(Ninjector.Get<IImageModel3D>()){ }

    public MyViewer(IImageModel3D imageModel3D)
    {
        this.InitializeComponent();

        this.ProjectedImageSource.Changed += ProjectedImageSource_Changed;
    }

    private void ProjectedImageSource_Changed(object sender, EventArgs e)
    {
        imageModel3D.SetImage((BitmapImage)sender);
    }

    public static readonly DependencyProperty ProjectedImageSourceProperty = DependencyProperty.Register(
                                                                    "ProjectedImageSource",
                                                                    typeof(BitmapSource),
                                                                    typeof(MyViewer));

    public BitmapSource ProjectedImageSource
    {
        get => (BitmapSource)this.GetValue(ProjectedImageSourceProperty);
        set => this.SetValue(ProjectedImageSourceProperty, value);
    }
}

我遇到的問題是,在構造函數中調用時ProjectedImageSource為null(我也曾在Loaded回調中嘗試過),因此我無法設置Changed事件處理程序。

我知道這里有一個ValidateValue回調,但這需要是一個靜態static,因此在這種情況下將無法使用。

我可以使用一些技巧來使此功能正常工作,還是從根本上來說有缺陷?

您應該使用依賴項屬性元數據注冊一個PropertyChangedCallback:

public static readonly DependencyProperty ProjectedImageSourceProperty =
    DependencyProperty.Register(
        nameof(ProjectedImageSource),
        typeof(BitmapSource),
        typeof(MyViewer),
        new PropertyMetadata(ProjectedImageSourceChanged));

private static void ProjectedImageSourceChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var viewer = (MyViewer)obj;

    viewer.imageModel3D.SetImage(e.NewValue as BitmapImage); // may not be a BitmapImage
}

或更短:

public static readonly DependencyProperty ProjectedImageSourceProperty =
    DependencyProperty.Register(
        nameof(ProjectedImageSource),
        typeof(BitmapSource),
        typeof(MyViewer),
        new PropertyMetadata(
            (o, e) => ((MyViewer)o).imageModel3D.SetImage(e.NewValue as BitmapImage)));

注意,您的IImageModel3D.SetImage方法似乎不需要BitmapImage參數。 最好改用BitmapSource ,然后像下面這樣編寫PropertyChangedCallback:

public static readonly DependencyProperty ProjectedImageSourceProperty =
    DependencyProperty.Register(
        nameof(ProjectedImageSource),
        typeof(BitmapSource),
        typeof(MyViewer),
        new PropertyMetadata(
            (o, e) => ((MyViewer)o).imageModel3D.SetImage((BitmapSource)e.NewValue)));

如果SetImage確實需要BitmapImage,則應更改依賴項屬性的類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM