【发布时间】:2020-12-07 04:11:51
【问题描述】:
我有以下ListBox 子类...
public class ImageAnnotationEditor : ListBox {
static ImageAnnotationEditor() {
DefaultStyleKeyProperty.OverrideMetadata(
typeof(ImageAnnotationEditor),
new FrameworkPropertyMetadata(typeof(ImageAnnotationEditor)));
}
#region ImageSource Property
public static readonly DependencyProperty ImageSourceProperty = Image.SourceProperty.AddOwner(
typeof(ImageAnnotationEditor));
public ImageSource? ImageSource {
get => (ImageSource)GetValue(ImageSourceProperty);
set => SetValue(ImageSourceProperty, value);
}
#endregion ImageSource Property
}
使用以下模板...
<ControlTemplate TargetType="{x:Type c:ImageAnnotationEditor}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Viewbox Stretch="Uniform"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<c:LayerPanel>
<Image Source="{TemplateBinding ImageSource}" /> <-- This line causes the problem
<StackPanel IsItemsHost="True" />
</c:LayerPanel>
</Viewbox>
</Border>
</ControlTemplate>
当我运行应用程序时,它会抛出以下内容...
System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'
如果我从ControlTemplate 中删除Source="{TemplateBinding ImageSource}",它会运行而不会崩溃,但当然不会显示图像。
【问题讨论】:
标签: c# wpf controltemplate imagesource templatebinding