0

I've got a DataTemplate which looks like this

<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>

I would like to replace the leafTextBlockKeyGesture by a TextBox if the IsEditing flag of the corresponding viewmodel is set to true. My Idea was to use a ContentControl inside the DataTemplate and change its Content depending on the IsEditing flag. I tried several solutions but I cannot find a working one.

Does anyone know how to do this?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
user3292642
  • 711
  • 2
  • 8
  • 32

1 Answers1

0

Based on this answer you need something like 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>
Community
  • 1
  • 1
lena
  • 1,181
  • 12
  • 36