C# 从父模板到样式的WPF绑定 信息

C# 从父模板到样式的WPF绑定 信息,c#,wpf,xaml,C#,Wpf,Xaml,我有两节课 问题类包含:IEnumerable答案和字符串对正 Answer类包含:stringAnswerType和booljusticationrequired 注意:我显示了该问题的最小代码 问题 我正在使用WPF。为了使事情模块化,我们正在使用样式和数据模板我试图将Question.xaml中的文本框可见性绑定到由样式触发的AnswerRadio.xaml中的答案需要的辩护属性 代码 问题: public class Question : IQuestion { public I

我有两节课

问题
类包含:
IEnumerable
答案和
字符串
对正

Answer
类包含:
string
AnswerType和
bool
justicationrequired

注意:我显示了该问题的最小代码

问题 我正在使用WPF。为了使事情模块化,我们正在使用
样式
数据模板
我试图将Question.xaml中的
文本框
可见性绑定到由
样式
触发的AnswerRadio.xaml中的
答案
需要的辩护属性

代码 问题:

public class Question : IQuestion
{
    public IEnumerable<Answer> Answers;
    public string Justification;
}
问题.xaml:

<ContentControl
    x:Name="AnswerControl"
    Style="{StaticResource AnswerTypeSelector}" />

<TextBox
    x:Name="txtJustification"
    Visibility="{Binding Path=Style, ElementName=AnswerControl, Converter={StaticResource BooleanToVisibilityCollapsedConverter}}"
    Text="{Binding Justification}" />
<TextBox
    x:Name="txtJustification"
    Visibility="{Binding Path=Tag.JustificationRequired, ElementName=AnswerControl, Converter={StaticResource BooleanToVisibilityCollapsedConverter}, FallbackValue=Collapsed}"
    Text="{Binding Justification}" />

AnswerTypeSelector.xaml:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../Templates/AnswerRadio.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="AnswerTypeSelector" TargetType="ContentControl">
    <Style.Triggers>
        <DataTrigger Binding="{Binding AnswerType}" Value="RadioButton">
            <Setter Property="Template" Value="{StaticResource AnswerRadioControl}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

AnswerRadio.xaml:

<Style x:Key="AnswerRadioItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <RadioButton x:Name="rbAnswer"
                        Content="{Binding Text}"
                        IsChecked="{Binding IsSelected, RelativeSource {x:Static RelativeSource.TemplatedParent}}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="AnswerRadioControl" TargetType="ContentControl">
    <StackPanel>
        <ListBox
        x:Name="lstQuestionRadioItem"
        ItemContainerStyle="{StaticResource AnswerRadioItem}"
        ItemsSource="{Binding Answers}"
        SelectedItem="{Binding SelectedAnswer}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </StackPanel>
</ControlTemplate>

找到了答案

与应答项
样式
类似,我们可以使用RelativeResource存储绑定并绑定到调用的
内容控件
,然后绑定到它的
[Tag][1]
属性

AnswerRadio.xaml

<ControlTemplate x:Key="AnswerRadioControl" TargetType="ContentControl">
    <StackPanel>
        <ListBox
        x:Name="lstQuestionRadioItem"
        ItemContainerStyle="{StaticResource AnswerRadioItem}"
        ItemsSource="{Binding Answers}"
        SelectedItem="{Binding Path=Tag, RelativeSource={RelativeSource AncestorType=ContentControl, Mode=FindAncestor, AncestorLevel=1}}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </StackPanel>
</ControlTemplate>
我认为将“SelectedAnswer”属性添加到问题类会使解决方案更简洁。如果你不想用这样的属性来污染你的模型类,那么考虑使用一个视图模型(参见MVVM模式)
<TextBox
    x:Name="txtJustification"
    Visibility="{Binding Path=Tag.JustificationRequired, ElementName=AnswerControl, Converter={StaticResource BooleanToVisibilityCollapsedConverter}, FallbackValue=Collapsed}"
    Text="{Binding Justification}" />