开发者

How to bind item values in an accordion header template

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'

<Grid x:Name="LayoutRoot">
    <Controls:Accordion Height="100">
        <my:AccordionItemWithIcons
            x:Name="FirstItem"
            Content="Content Text" 
            Header="Header Text" 
            CheckIsVisible="Collapsed" 
            EventSummary="Summary Text" 
            HeaderTemplate="{StaticResource AccordionItemHeaderTemplate1}"/>
    </Controls:Accordion>
</Grid>

And here is the header template.

<DataTemplate x:Key="AccordionWithIcons_HeaderTemplate1" >
    <Grid  >            
        <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
            <TextBlock Text="{Binding EventSummary}" />
            <Image Visibility="{Binding CheckIsVisible}" Source="/Labyrinth;component/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've spent hours messing about with different DataContext's and sources and cannot seem to get this to work!

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.

FirstItem.CheckIsVisible = Visibility.Visible  


For this, there exists a VisibilityToBooleanConverter

<BooleanToVisibilityConverter x:Key=”boolVisConverter”/>

[...]

Visibility="{Binding ElementName=anyCheckbox,
    Path=IsChecked,
    Converter={StaticResource boolVisConverter}}"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜