2011-03-30 4 views
1

내가 가진 간단한 클래스로 바인딩을 수행하는실패 이미지

public class A 
{ 
    public ImageSource imageSource 
    { 
     get; 
     set; 
    } 
} 

페이지 클래스 :이 페이지에서 개체 유형 A. 를 포함

Public class page : Page 
{ 
    A a_class = new A(); 
} 

그리고 간단한 실버 페이지가 내가 원하는 이미지를 A의 imageSource에 바인딩 할 수 있습니다.

그래서 나는 그것을 썼다.

<Image x:Name="Image_" Stretch="Fill" 
     Source="{Binding imageSource}" DataContext="{StaticResource a_class }"/> 

어떻게 작성해야 제대로 작동할까요?

도움 주셔서 감사합니다.

+0

"작동하지 않음"이란 무엇입니까? 오류가 있습니까? 이미지가 보이지 않습니까? imageSource를 설정하는 코드는 표시하지 않습니다. – cadrell0

답변

1

StaticResource 마크 업 확장은 Xaml이로드되는 클래스의 필드 또는 속성에 액세스하지 않습니다. 라인을 삭제합니다 -

대신
A a_class = new A(); 

예 A를 리소스 사전에 : -

<UserControl x:Class="YourApplication.UserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns:local="clr-namespace:YourApplication" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <UserControl.Resources> 
     <local:A x:Key="a_class" /> 
    </UserControl> 
    <Grid x:Name="LayoutRoot"> 
     <Image x:Name="Image_" Stretch="Fill" 
      Source="{Binding imageSource}" DataContext="{StaticResource a_class}"/> 
    </Grid> 
</UserControl> 

참고하면 이미지 컨트롤은 당신이 INotifyPropertyChanged을 구현하는 A을 필요로하는 imageSource 속성에 대한 변경 사항을 추적 할 수 있습니다.