在WPF中绑定图像? [英] Binding an Image in WPF?

查看:141
本文介绍了在WPF中绑定图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF中显示一个由进程创建的图像,

例如:我们有一个名为createWPFImage()的方法

 图像createWPFImage(){...} 

所以,createWPFImage()的输出是一个图像。

在XAML代码中,我们有一个如下所示的东西:

 < StackPanel.ToolTip> 
< StackPanel Orientation =Horizo​​ntal>
< Image Width =64Height =64Margin =0 2 4 0/>
< TextBlock Text ={Binding Path = Description}VerticalAlignment =Center/>
< / StackPanel>
< /StackPanel.ToolTip>



现在,如何将createWPFImage()的输出绑定到XAML代码中的图像?

如果您指导我,我将不胜感激。

解决方案

p>说你有类MyClass,方法CreateWpfImage(见下面的例子)



在你的XAML中,你可以创建MyClass,然后使用ObjectDataProvider调用CreateWpfImage在资源部分(参见Bea Stollnitz博客文章 ObjectDataProvider ) 。



XAML

 < Window x:Class =MyApplicationNamespace .Window1
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006 / xaml
xmlns:MyApplicationNamespace =clr-namespace:MyApplicationNamespace
Title =Window1Height =300Width =300>

< Window.Resources>
< ObjectDataProvider ObjectType ={x:Type MyApplicationNamespace:MyClass}x:Key =MyClass/>
< ObjectDataProvider ObjectInstance ={StaticResource MyClass}MethodName =CreateWpfImpagex:Key =MyImage/>
< /Window.Resources>

< StackPanel>
< Image Source ={Binding Source = {StaticResource MyImage},Path = Source}/>
< / StackPanel>



示例MyClass代码创建使用XAML的图像 -

  using System; 
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;

命名空间MyApplicationNamespace
{
public class MyClass
{
public Image CreateWpfImpage()
{
GeometryDrawing aGeometryDrawing = new GeometryDrawing ();
aGeometryDrawing.Geometry = new EllipseGeometry(new Point(50,50),50,50);
aGeometryDrawing.Pen = new Pen(Brushes.Red,10);
aGeometryDrawing.Brush = Brushes.Blue;
DrawingImage geometryImage = new DrawingImage(aGeometryDrawing);

图像anImage = new Image();
anImage.Source = geometryImage;
return anImage;
}
}
}


I wanna show an image in WPF that is created by a process,
e.g : we have a method that is named createWPFImage()

Image createWPFImage() { ... }

So, the output of createWPFImage() is an Image.
In the XAML code, we have a something like below :

<StackPanel.ToolTip>
    <StackPanel Orientation="Horizontal">
        <Image Width="64" Height="64" Margin="0 2 4 0" />
        <TextBlock Text="{Binding Path=Description}" VerticalAlignment="Center" />
    </StackPanel>
</StackPanel.ToolTip>


Now, How can I bind the output of createWPFImage() to the Image in XAML code ?
I would be appreciate if you guide me.

解决方案

Say you have class "MyClass" with method "CreateWpfImage" (see example below).

In your XAML you can create MyClass, and then call CreateWpfImage, using ObjectDataProvider in a Resources section (See Bea Stollnitz blog article ObjectDataProvider).

XAML

<Window x:Class="MyApplicationNamespace.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyApplicationNamespace="clr-namespace:MyApplicationNamespace"
    Title="Window1" Height="300" Width="300">       

<Window.Resources>
    <ObjectDataProvider ObjectType="{x:Type MyApplicationNamespace:MyClass}" x:Key="MyClass" />
    <ObjectDataProvider ObjectInstance="{StaticResource MyClass}" MethodName="CreateWpfImpage" x:Key="MyImage" />
</Window.Resources>

<StackPanel>
    <Image Source="{Binding Source={StaticResource MyImage}, Path=Source}"/>
</StackPanel>

Example MyClass code to create an image for the XAML to use -

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyApplicationNamespace
{
    public class MyClass
    {
        public Image CreateWpfImpage()
        {
            GeometryDrawing aGeometryDrawing = new GeometryDrawing();
            aGeometryDrawing.Geometry = new EllipseGeometry(new Point(50, 50), 50, 50);
            aGeometryDrawing.Pen = new Pen(Brushes.Red, 10);
            aGeometryDrawing.Brush = Brushes.Blue;
            DrawingImage geometryImage = new DrawingImage(aGeometryDrawing);

            Image anImage = new Image();
            anImage.Source = geometryImage;
            return anImage;
        }
    }
}

这篇关于在WPF中绑定图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆