C# 如何使用ContentPresenter在自定义控件中显示位图图像?
这方面我还不太熟悉。我正在尝试在WPF中创建一个自定义控件,它将在下面显示一个图像和一段文本。我以BitmapImage类型从url下载图像。我在我的UI上有一个测试,以测试它是否正确下载,它是否正确。它下载并显示在中,但在自定义控件中,Icon-dependency属性只是显示URL,我假设URL是它看到的内容的ToString 左边的功能框用于确认图像是否正确进入,右边的GameIconControl出现故障,其外观如下: 下面是控件的Generic.xamlC# 如何使用ContentPresenter在自定义控件中显示位图图像?,c#,wpf,custom-controls,contentpresenter,C#,Wpf,Custom Controls,Contentpresenter,这方面我还不太熟悉。我正在尝试在WPF中创建一个自定义控件,它将在下面显示一个图像和一段文本。我以BitmapImage类型从url下载图像。我在我的UI上有一个测试,以测试它是否正确下载,它是否正确。它下载并显示在中,但在自定义控件中,Icon-dependency属性只是显示URL,我假设URL是它看到的内容的ToString 左边的功能框用于确认图像是否正确进入,右边的GameIconControl出现故障,其外观如下: 下面是控件的Generic.xaml <Style Targe
<Style TargetType="{x:Type assets:GameIconControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type assets:GameIconControl}">
<Border Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Vertical">
<ContentPresenter Height="Auto"
Margin="3"
ContentSource="Icon"
HorizontalAlignment="Center"/>
<ContentPresenter Height="Auto"
Margin="3"
ContentSource="GameName"
HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我缺少什么?我建议使用图像来显示图标属性,而不是ContentPresenter。将Icon属性绑定到Image.Source,它应该可以工作。您应该可以使用ImageSource属性绑定到图像。要显示它,Source属性:
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(IconPropertyName, typeof(ImageSource),
typeof(GameIconControl), new PropertyMetadata(null));
...
Icon = new BitmapImage(new Uri("C:\\Image.jpg"));
...
<Image Source="{TemplateBinding Icon}" />
应为ControlTemplate中的TemplateBinding。
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(IconPropertyName, typeof(ImageSource),
typeof(GameIconControl), new PropertyMetadata(null));
...
Icon = new BitmapImage(new Uri("C:\\Image.jpg"));
...
<Image Source="{TemplateBinding Icon}" />
Icon = "C:\\Image.jpg";
or
Icon = "/YourAppName;component/ImageFolderName/ImageName.jpg";
...
<Image Source="{TemplateBinding Icon}" />