silverlight-2.0 – 在Silverlight的列表框中访问父数据文件

silverlight-2.0 – 在Silverlight的列表框中访问父数据文件,第1张

概述在Silverlight 2中,我使用的用户控件继承了嵌入页面的数据报文。此数据文本包含问题文本,问题类型和答案集合。在用户控件中是一个列表框,它绑定到答案集合。如下所示: <ListBox DataContext="{Binding}" x:Name="AnswerListBox" ItemContainerStyle="{StaticResource QuestionStyle}" Grid. 在Silverlight 2中,我使用的用户控件继承了嵌入页面的数据报文。此数据文本包含问题文本,问题类型和答案集合。在用户控件中是一个列表框,它绑定到答案集合。如下所示:

<ListBox DataContext="{Binding}" x:name="AnswerListBox" ItemContainerStyle="{StaticResource QuestionStyle}" GrID.Row="1" WIDth="auto" GrID.Column="2" ItemsSource="{Binding Path=AnswerList}" borderBrush="{x:Null}" />

此列表框具有关联的样式,以单选按钮或复选框(我想根据问题类型隐藏或显示)的形式显示答案:

<Style targettype="ListBoxItem" x:Key="QuestionStyle">            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate targettype="ListBoxItem">                                              <StackPanel Background="transparent" >                            <Radiobutton Visibility="{Binding Path=QuestionType,Converter={StaticResource QuestionTypeConverter},ConverterParameter='RadiobuttonStyle'}" Height="auto" margin="0,10"  IsChecked="{TemplateBinding IsSelected}" IsHitTestVisible="False" Content="{Binding Path=AnswerText}">                            </Radiobutton>                            <CheckBox Visibility="{Binding Path=QuestionType,ConverterParameter='CheckBoxStyle'}" Height="auto" margin="0,10" Content="{Binding Path=AnswerText}">                            </CheckBox>                        </StackPanel>                                                                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>

所以我的问题是:你如何访问父数据上下文为了得到QuestionType(因为这是一个属性在用户控制数据报文本身,而不是一个属性在AnswerList的AnswerItem)?

或者有一个更好的方法在xaml中基于绑定值动态切换样式?

解决方法 在Silverlight 3和更高版本中,您可以使用元素到元素绑定并指向控件DataContext,然后在我的示例中的一些属性的Threshold属性。

所以首先命名你的控件(例如在我的例子中它的x:name =“control”)

<UserControl x:Class="SomeApp.VIEws.MainPageVIEw" x:name="control" >

然后在这个控件在你的ListBox ItemTemplate你可以访问父DataContext像这样:

<ListBox ItemsSource="{Binding Path=SomeItems}" >        <ListBox.ItemTemplate>        <DataTemplate>            <StackPanel OrIEntation="Horizontal">            <TextBlock Text="{Binding Elementname=control,Path=DataContext.Threshold}"/>            </StackPanel>        </DataTemplate>       </ListBox.ItemTemplate>                </ListBox>
总结

以上是内存溢出为你收集整理的silverlight-2.0 – 在Silverlight的列表框中访问父数据文件全部内容,希望文章能够帮你解决silverlight-2.0 – 在Silverlight的列表框中访问父数据文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1008305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存