在 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 中动态切换样式?