如何在手风琴标题模板中绑定项目值 [英] How to bind item values in an accordion header template

查看:91
本文介绍了如何在手风琴标题模板中绑定项目值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Silverlight 4中为手风琴对象创建一个简单的标题模板.我已经向AccordionItem的标题模板添加了一个图像和一个TextBlock.
我想隐藏或显示图像,具体取决于页面上输入的值.因为我想将这些值直接绑定到实际的手风琴项目,所以我创建了一个新类型"AccordionItemWithIcons",该类型简单地继承自AccordionItem,但添加了一些依赖属性来处理此问题.为了简洁起见,我仅展示其中的几个属性. :)

因此,这是我的手风琴和"AccordionItemWithIcons"控件. 请注意,属性"CheckIsVisible"的类型为"Visibility"

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

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



谁能解释我如何将TextBlock的文本和Image的可视性绑定到基础AccordionItemWithIcons对象中设置的值?
我不知道是否能帮助解释我要实现的目标,但是最终在背后的代码中,我希望能够说出类似(如下所示)的内容,以显示或隐藏标题模板中的图标.



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.

FirstItem.CheckIsVisible = Visibility.Visible
SecondItem.CheckIsVisible = Visibility.Collapsed
...etc...

推荐答案



也许您想尝试简单的数据绑定.

假设您已为AccordionItem的DataContext分配了一个对象,该对象包含所有必要属性(即EventSummaryText,CheckIsVisible ...),因此您可以绑定AccordionItem的属性.
现在,您可以通过声明AccordionItem的标头将"DataContext"非常传输"到您的HeaderTemplate,如下所示
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
Header="{Binding}"


您可以在模板中执行简单的DataBinding.

顺便说一句:
如果您提供了完整的XAML代码,则无需在DataTemplate中使用Grid.

干杯


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


这篇关于如何在手风琴标题模板中绑定项目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆