【发布时间】:2008-12-22 08:25:10
【问题描述】:
在 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,0,0,10" IsChecked="{TemplateBinding IsSelected}" IsHitTestVisible="False" Content="{Binding Path=AnswerText}">
</RadioButton>
<CheckBox Visibility="{Binding Path=QuestionType, Converter={StaticResource QuestionTypeConverter}, ConverterParameter='CheckBoxStyle'}" Height="auto" Margin="0,0,0,10" Content="{Binding Path=AnswerText}">
</CheckBox>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
那么我的问题是:如何访问父数据上下文以获取 QuestionType(因为这是用户控件数据上下文本身的属性,而不是 AnswerList 中 AnswerItem 的属性)?
或者,有没有更好的方法可以根据绑定值在 xaml 中动态切换样式?
【问题讨论】:
-
您有什么方法可以更改您接受的答案吗?下面是 Roboblob 的一个正确答案。接受的答案对于 Silverlight 是不正确的并且具有误导性。
标签: silverlight-2.0