Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to create a simple header template for an accordion object in silverlight 4. I've added an image and a TextBlock to the header template of the AccordionItem.
I want to hide or show the image dependant on the values entered on the page. Because i want to bind these values directly to the actual accordion item, I've created a new type 'AccordionItemWithIcons' that simply inherits from AccordionItem but adds a couple of dependancy properties to handle this. I'm only showing a couple of those properties for brevity. :)

So, here's my accordion with my 'AccordionItemWithIcons' control. Note that the property 'CheckIsVisible' is of type 'Visibility'

HTML
<grid x:name="LayoutRoot" xmlns:x="#unknown">
    <controls:accordion height="100" xmlns:controls="#unknown">
        <my:accordionitemwithicons xmlns:my="#unknown">
            x:Name="FirstItem"
	    Content="Content Text" 
	    Header="Header Text" 
    	    CheckIsVisible="Collapsed" 
    	    EventSummary="Summary Text" 
    	    HeaderTemplate="{StaticResource AccordionItemHeaderTemplate1}"/>
    </my:accordionitemwithicons></controls:accordion>
</grid>


And here is the header template.

HTML
<datatemplate x:key="AccordionWithIcons_HeaderTemplate1" xmlns:x="#unknown">
    <grid>        	
        <stackpanel orientation="Horizontal" verticalalignment="Top">
            <textblock text="{Binding EventSummary}" />
            <Image Visibility="{Binding CheckIsVisible}" Source="/cross.png"/>
        </stackpanel>
    </grid>
</datatemplate>


Can anyone explain how I can bind the TextBlock's text and the Image's Visibility to the values set in the underlying AccordionItemWithIcons object?

I don't know if helps to explain what I'm trying to achieve, but ultimately in the code behind i want to be able to say something like (shown below), to show or hide the icon in the header template.

C#
FirstItem.CheckIsVisible = Visibility.Visible
SecondItem.CheckIsVisible = Visibility.Collapsed
...etc...
Posted
Updated 8-Sep-11 0:14am
v2

1 solution

Hi,

maybe you want to try simple data binding.

Assuming you have assigned an object to the AccordionItem's DataContext, which contains all the neccessary properties (i. e. EventSummaryText, CheckIsVisible...) so you can bind the AccordionItem's properties.
Now you can 'transport' that very DataContext to your HeaderTemplate by declaring the Header of the AccordionItem as follows
XAML
Header="{Binding}"

and you can do simple DataBinding in your template.

By the way:
There is no need for the Grid in your DataTemplate (if you provided the full XAML-code).

Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900