开发者

Fail to do binding to image

开发者 https://www.devze.com 2023-02-20 15:05 出处:网络
i have simple class public class A { public ImageSource imageSource { get; set; } } page class: Public class page : Page

i have simple class

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

page class:

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

And simple silverlight page that contain object type A. In this page i have Image that i want to bind to imageSource of A.

So i wrote it and its not working.

 <Image x:Name="Image_" Stretch="Fill" 
        Source开发者_高级运维="{Binding imageSource}"  DataContext="{StaticResource a_class }"/>

How i need to write it so it will work fine ?

Thanks for any help.


The StaticResource markup extension doed not access fields or properties of the class that the Xaml is loaded into. Delete the line:-

 A a_class = new A();

Instead instance A in a resource dictionary:-

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

Note is you want the Image control to track changes made to the imageSource property you need A to implement INotifyPropertyChanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消