2011-03-30 64 views
1

我有简单的类挡不结合图像

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

页类:

Public class page : Page 
{ 
    A a_class = new A(); 
} 
包含对象类型A 在本页面

而且简单Silverlight页面我有我想要的图像绑定到A的imageSource。

所以我写了它,它不工作。

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

我该怎么写才能正常工作?

感谢您的任何帮助。

+0

什么是“不工作”?你有错误吗?图像没有显示?您不会在设置imageSource的位置显示任何代码。 – cadrell0 2011-03-30 15:11:48

回答

1

StaticResource标记扩展不会访问Xaml加载到的类的字段或属性。删除行: -

A a_class = new 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> 

注意是你想要的图像控制来跟踪你需要A实现INotifyPropertyChanged到ImageSource的财产所做的更改。