根据XElement中的Attribute更改DataTemplate

时间:2012-07-13 19:29:00

标签: wpf xaml .net-4.0 linq-to-xml

好的我有一个看起来像的XElement:

<authentication mode="Forms">
  <forms loginUrl="login.aspx" name=".LOGIN" protection="All" timeout="4800" path="/" />
</authentication>

然后在我的XAML中,我设置了一个类似于:

的ContentControl
<ContentControl Content="{Binding Data}">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=Attribute[mode].Value}" Value="Forms">
                        <Setter Property="ContentTemplate" Value="{StaticResource FormsTemplate}"/>                            
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>

Data是我的包含XElement的公共属性。我的模板看起来像:

<DataTemplate x:Key="FormsTemplate">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Login URL"/>
                <TextBox Text="{Binding Path=Element[forms].Attribute[loginUrl].Value}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name"/>
                <TextBox Text="{Binding Path=Attribute[name].Value}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Protection"/>
                <TextBox Text="{Binding Path=Attribute[protection].Value}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Timeout"/>
                <TextBox Text="{Binding Path=Attribute[timeout].Value}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Path"/>
                <TextBox Text="{Binding Path=Attribute[path].Value}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    <DataTemplate DataType="passport">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Redirect URL"/>
                <TextBox Text="{Binding Path=Attribute[redirectUrl].Value}"/>
            </StackPanel>                
        </StackPanel>
    </DataTemplate>

为什么这不起作用?当我这样做时,屏幕上什么都没有出现。

1 个答案:

答案 0 :(得分:0)

我通过使用样式触发器来解决它。以下是我必须使用的工具。

<ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Data.Attribute[mode].Value}" Value="Forms">
                        <Setter Property="ContentTemplate" Value="{StaticResource FormsTemplate}"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Data.Attribute[mode].Value}" Value="Passport">
                        <Setter Property="ContentTemplate" Value="{StaticResource PassportTemplate}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>