【发布时间】:2018-02-15 11:20:39
【问题描述】:
我正在尝试实现一个将图像投影到 3D 模型上的 WPF 组件。为了使其使用起来更整洁,我尝试使用依赖属性,以便我可以通过以下方式从我的视图中绑定控件:
<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 回调,但这需要是一个静态静态,因此在这种情况下不起作用。
我可以使用一些技巧来使此功能正常工作,还是尝试这样做存在根本缺陷?
【问题讨论】:
标签: c# wpf dependency-properties