2010-02-04 6 views
3

나는 과정에 의해 생성되는 WPF에서 이미지를 보여 싶어
예컨대 우리가 createWPFImage라는 방법()
WPF에서 이미지 바인딩?

Image createWPFImage() { ... } 

그래서, createWPFImage의 출력() 이미지입니다 있습니다.
지금

<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> 


은 어떻게 XAML 코드에서 이미지에 createWPFImage()의 출력을 결합 할 수있는 다음 XAML 코드에서
, 우리는 다음과 같은 것이 있나요?
나를 안내해 주시면 감사하겠습니다.

+0

"바인딩"이란 무엇을 의미합니까? WPF에서 "바인딩"은 원본 값이 변경 될 때 대상 값을 변경하는 것을 의미합니다 (또는 그 반대로, 일회성 바인딩 제외). 아니면 그냥 메서드의 출력을 이미지로 설정하고 싶습니까? 메서드가 다른 조건에 따라 다른 출력을 반환합니까? 종속성 속성을 사용해 보셨습니까? – mg007

답변

4

"CreateWpfImage"메서드를 사용하여 클래스 "MyClass"가 있다고합시다 (아래 예제 참조).

XAML에서 리소스 섹션의 ObjectDataProvider를 사용하여 MyClass를 만든 다음 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="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> 

XAML을 사용하는 이미지를 만들 수

예 MyClass의 코드 - 당신이 당신의 이미지에 대한 경로가있는 경우

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; 
     } 
    } 
} 
+0

ObjectDataProvider는 정적이지만 동적으로 처리 할 예정입니다. –

2

을 바로 할 수 있도록하려면 즉석에서 이미지를 변경 한 다음 string 유형의 종속성 속성에 바인딩하고 메서드에서 종속성 속성의 값을 설정합니다.

<Image Source="{Binding MyImagePath}" /> 

    public static readonly DependencyProperty MyImagePathProperty = DependencyProperty.Register("MyImagePath", typeof(string), typeof(ClassName), new PropertyMetadata("pack://application:,,,/YourAssembly;component//icons/icon1.png")); 


    public string MyImagePath 
    { 
     get { return (string)GetValue(MyImagePathhProperty); } 
     set { SetValue(MyImagePathProperty, value); } 
    }