触发ContentControl的内容

时间:2017-01-17 11:54:02

标签: c# wpf

我有DataTemplate看起来像这样

<DataTemplate DataType="{x:Type viewModel:TreeViewLeafViewModel}">
    <StackPanel Orientation="Horizontal">
        <Image Name="leafImage"/>
        <TextBlock Name="leafTextBlockDisplayName" VerticalAlignment="Center"/>
        <TextBlock Name="leafTextBlockKeyGesture" VerticalAlignment="Center"/>
    </StackPanel>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Row, Converter={StaticResource MatchTypeConverter},
                ConverterParameter={x:Type viewModel:TreeViewLeafViewModel}}" Value="True">
            <Setter Property="Source" TargetName="leafImage" Value="{Binding Path=Row.Icon, Mode=OneTime}" />
            <Setter Property="Text" TargetName="leafTextBlockDisplayName" Value="{Binding Path=Row.DisplayName, Mode=OneTime}" />
            <Setter Property="Text" TargetName="leafTextBlockKeyGesture" Value="{Binding Path=Row.KeyGesture.KeyModifierString, Mode=OneTime}" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

如果相应viewmodel的leafTextBlockKeyGesture标志设置为true,我想将TextBox替换为IsEditing。我的想法是在ContentControl内使用DataTemplate,并根据Content标记更改其IsEditing。我尝试了几种解决方案但我找不到合适的解决方案。

有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

根据this回答你需要这样的事情:

<StackPanel>
    <StackPanel.Resources>
        <DataTemplate x:Key="textbox">
            <TextBox Text="edit me"/>
        </DataTemplate>
        <DataTemplate x:Key="textblock">
            <TextBlock Text="can't edit"/>
        </DataTemplate>
    </StackPanel.Resources>
    <CheckBox IsChecked="{Binding IsEditable}" Content="Editable"/>
    <ContentControl Content="{Binding}">
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate" Value="{StaticResource textblock}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsEditable}" Value="true">
                        <Setter Property="ContentTemplate" Value="{StaticResource textbox}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</StackPanel>