Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

How to implement databinding like this?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-13-2006   #1 (permalink)
Leo Xue
Guest


 

How to implement databinding like this?

I store the file name of images in "PIC" field.
Then I bind the filed to Source property of an Image.
<Image Source="{Binding PIC}"/>
But since only file name is stored, so the path of the
image files should also be added to the Source.
What should I do to binding this correctly?
I have tried in this way,
<Image Source="Images/"+"{Binding PIC}"/>
But it seems to be a wrong syntax.

Any suggestion?



My System SpecsSystem Spec
Old 11-14-2006   #2 (permalink)
Bob
Guest


 

Re: How to implement databinding like this? (long)

In article <e074h7yBHHA.3928@TK2MSFTNGP03.phx.gbl>, xueting_cn@sohu.com
says...
> I store the file name of images in "PIC" field.
> Then I bind the filed to Source property of an Image.
> <Image Source="{Binding PIC}"/>
> But since only file name is stored, so the path of the
> image files should also be added to the Source.
> What should I do to binding this correctly?
> I have tried in this way,
> <Image Source="Images/"+"{Binding PIC}"/>
> But it seems to be a wrong syntax.
>
> Any suggestion?
>

I think you have a picture name stored in an "old fashioned" .NET
property (or public field). I also think you would like to dynamically
change the value of that fieldd and have your Image automagically
updated as a result. I messed around with this (and got a bit smarter)
so I'll share. (sorry if too long).

You can bind the Source property of an Image to the .NET property but
the Image will not be updated when the underlying data changes. To get
that functionality you need to implement INotifyPropertyChanged
interface.
The example below displays 2 Images and a button. Both Images are set to
the same jpg and then in the Button's click handler the underlying
sources are both changed. The result is the first image remains the same
and the second image changes as desired.
(You will need two images in a directory named 'Images' to run the
sample.)

Watch for wordwrap!

Bob

------------ XAML -------------------------

<Window x:Class="databind4Image.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:databind4Image"
Title="databind4Image"
SizeToContent="WidthAndHeight"
>

<Window.Resources>
<localemoData x:Key="Demo"/>
<Style TargetType="{x:Type Image}">
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="200"/>
</Style>
</Window.Resources>
<StackPanel DataContext="{Binding Source={StaticResource Demo}}">
<Button Name="button1" Click="button1_Click">Change
Images' Source Value</Button>
<Image Name ="image1" Source="{Binding
Path=Picture_filename, Mode=OneWay}" />
<Image Name ="image2" Source="{Binding Path=Pic_name,
Mode=OneWay}" />
</StackPanel>
</Window>

-------- codebehind --------

using System;
using System.Windows;
namespace databind4Image
{
public partial class Window1 : System.Windows.Window
{
private string picture_filename;

public string Picture_filename
{
get { return picture_filename; }
set { picture_filename = value; }
}

public Window1()
{
InitializeComponent();
this.Picture_filename = @"/Images/wpaflag.jpg";
image1.DataContext = this;
button1.Click += new RoutedEventHandler(button1_Click);
}

void button1_Click(object sender, RoutedEventArgs e)
{
Picture_filename = @"/Images/wpaflag2.jpg";
DemoData demodata = (DemoData)this.FindResource("Demo");
demodata.Pic_name = @"/Images/wpaflag2.jpg";
}
}
}

------- Data Object -----------------

using System;
using System.ComponentModel; // for INotifyPropertyChanged

namespace databind4Image
{
public class DemoData : INotifyPropertyChanged
{
private string pic_name;
public event PropertyChangedEventHandler PropertyChanged;

public DemoData()
{
Pic_name = @"/Images/wpaflag.jpg";
}

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs
(info));
}
}

public string Pic_name
{
get{return this.pic_name;}
set{
if (value != this.pic_name)
{
this.pic_name = value;
NotifyPropertyChanged("Pic_name");
}
}
}
}
}
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Headphones setting? Where is it? Please implement! bagnon Vista General 1 04-22-2008 06:01 PM
Implement ICommandSource Sample in VB Horst Klein Avalon 0 01-12-2007 10:32 AM
how to implement a CheckedListbox (or something similar) Sam Jost Avalon 1 09-25-2006 08:24 AM
How to implement customized TriggerAction HolaMan Avalon 0 09-08-2006 06:08 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51