Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Data binding of Button.Content to an image in a XML island does not work

 
 
Old 04-04-2007   #1 (permalink)
muchinger@web.de


 
 

Data binding of Button.Content to an image in a XML island does not work

All,

this code works fine. You may past it into XAMLPad or similar and you
should see a button with green background and an image on it (well you
should make sure that you have an icon named "about.ico" in c:\)

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="DataProvider" XPath="Data">
<x:XData>
<Data xmlns="">
<Control Name="BTN_1">
<Background>Green</Background>
</Control>
</Data>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Page.DataContext>
<Binding Source="{StaticResource DataProvider}" />
</Page.DataContext>
<Button x:Name="BTN_1" Height ="100" Width="100">
<Button.Content>
<Image Source ="C:\about.ico" />
</Button.Content>
<Button.Background>
<Binding XPath="Control[@Name='BTN_1']/Background" />
</Button.Background>
</Button>
</Page>

As you can see I data bind the Button.Background property to a value
in the XML island. The same I want to do with the Button.Content. So
this is what I tried.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="DataProvider" XPath="Data">
<x:XData>
<Data xmlns="">
<Control Name="BTN_1">
<Background>Green</Background>
<Content>
<Image Source ="C:\about.ico" />
</Content>
</Control>
</Data>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Page.DataContext>
<Binding Source="{StaticResource DataProvider}" />
</Page.DataContext>
<Button x:Name="BTN_1" Height ="100" Width="100">
<Button.Content>
<Binding XPath="Control[@Name='BTN_1']/Content" />
</Button.Content>
<Button.Background>
<Binding XPath="Control[@Name='BTN_1']/Background" />
</Button.Background>
</Button>
</Page>

If you paste this code into XAMLPad you will see a button with green
background only. The image is not set as the content of the button.
Why this?
Is my approach too naive? Do I have to take a different one like
applying a control template in order to change the button content?
Just for a little background: why am I doing this all? Goal is to have
a page with a button on it. Once the page is rendered I want to be
able to just switch the page's data context to another xml data island
and have the button display a new image. Basically this is my notion
of separating presentation and data. The presentation data binds to
the data and the latter is easily interchangeable.

Thanks for any input,
Michael


My System SpecsSystem Spec
Old 04-17-2007   #2 (permalink)
tbrummel


 
 

RE: Data binding of Button.Content to an image in a XML island does no

I admit to not having tried this and I've done nothing with XML datasources
so I'm a bit out of my waters... but at first glance it seems that you'd end
up with a string as the content of your button (but you didn't say that so
I'm just stabbing around here).

The XML binding result is a string, right? So, you're assigning a string to
the content of the button -- granted the string is "<Image... >" but it's not
a control. The color works because there is a value converter that converts
strings (green) into brushes.

Try using your first code example but binding the Image's Source property to
a string you get from the XML. That should work since the image is then
instantiated by the XAML parser and the Source property will be databound.

<Button.Content>
<Image Source="{Binding XPath=... }" />
</Button.Content>


"muchinger@web.de" wrote:

> All,
>
> this code works fine. You may past it into XAMLPad or similar and you
> should see a button with green background and an image on it (well you
> should make sure that you have an icon named "about.ico" in c:\)
>
> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
> presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
> <Page.Resources>
> <XmlDataProvider x:Key="DataProvider" XPath="Data">
> <x:XData>
> <Data xmlns="">
> <Control Name="BTN_1">
> <Background>Green</Background>
> </Control>
> </Data>
> </x:XData>
> </XmlDataProvider>
> </Page.Resources>
> <Page.DataContext>
> <Binding Source="{StaticResource DataProvider}" />
> </Page.DataContext>
> <Button x:Name="BTN_1" Height ="100" Width="100">
> <Button.Content>
> <Image Source ="C:\about.ico" />
> </Button.Content>
> <Button.Background>
> <Binding XPath="Control[@Name='BTN_1']/Background" />
> </Button.Background>
> </Button>
> </Page>
>
> As you can see I data bind the Button.Background property to a value
> in the XML island. The same I want to do with the Button.Content. So
> this is what I tried.
>
> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
> presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
> <Page.Resources>
> <XmlDataProvider x:Key="DataProvider" XPath="Data">
> <x:XData>
> <Data xmlns="">
> <Control Name="BTN_1">
> <Background>Green</Background>
> <Content>
> <Image Source ="C:\about.ico" />
> </Content>
> </Control>
> </Data>
> </x:XData>
> </XmlDataProvider>
> </Page.Resources>
> <Page.DataContext>
> <Binding Source="{StaticResource DataProvider}" />
> </Page.DataContext>
> <Button x:Name="BTN_1" Height ="100" Width="100">
> <Button.Content>
> <Binding XPath="Control[@Name='BTN_1']/Content" />
> </Button.Content>
> <Button.Background>
> <Binding XPath="Control[@Name='BTN_1']/Background" />
> </Button.Background>
> </Button>
> </Page>
>
> If you paste this code into XAMLPad you will see a button with green
> background only. The image is not set as the content of the button.
> Why this?
> Is my approach too naive? Do I have to take a different one like
> applying a control template in order to change the button content?
> Just for a little background: why am I doing this all? Goal is to have
> a page with a button on it. Once the page is rendered I want to be
> able to just switch the page's data context to another xml data island
> and have the button display a new image. Basically this is my notion
> of separating presentation and data. The presentation data binds to
> the data and the latter is easily interchangeable.
>
> Thanks for any input,
> Michael
>
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Can't get Monkey Island 4 to work on Vista Gaming
Lego Island Wont Work On Vista Vista Games


Vista Forums 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 Ltd

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