2011-03-30 60 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的財產所做的更改。