Explicit use of TextBlock in shortcut content makes ContentPresenter behave strangely

I have a ControlTemplatedouble ContentPresenters custom . The pattern applies to Label. When I set "Random Octopus" (just text) as the content of the label, it works exactly as expected. When I set "& lt; TextBlock & gt; Random Octopus & lt; / TextBlock & gt;" as Content, it does not work (only one ContentPresenter is visually represented). I use the following code to reproduce the behavior:

<Window x:Class="WeirdTextBlock.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Window.Resources>
        <Style TargetType="Label">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Label">
                        <Border BorderBrush="Red" BorderThickness="1" Padding="2">
                            <Grid>
                                <ContentPresenter />
                                <ContentPresenter Margin="2,2,0,0" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid Margin="20" HorizontalAlignment="Left">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="20" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Grid.Row="0">
            Random octopus
        </Label>

        <Label Grid.Row="2">
            <TextBlock>Random octopus</TextBlock>
        </Label>
    </Grid>
</Window>

And here you can see how it looks:

, , Content, TextBlock, Label ? Label ( Label, )? !

+3
2

, ...

  • Content Label, ContentPresenter TextBlock.
  • TextBlock Content Label, , ContentPresenter, TextBlock, .

<Style TargetType="Label">
    <Style.Resources>
        <local:TypeOfConverter x:Key="TypeOfConverter"/>
        <Style TargetType="TextBlock">
            <Setter Property="Background" Value="Transparent"/>
        </Style>
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <Border BorderBrush="Red" BorderThickness="1" Padding="2">
                    <Grid>
                        <ContentPresenter Name="content" Grid.ZIndex="2"/>
                        <ContentPresenter Name="secondContent" Grid.ZIndex="1" Margin="2,2,0,0" Visibility="Collapsed"/>
                        <Border Grid.ZIndex="1">
                            <Border.RenderTransform>
                                <TranslateTransform X="2" Y="2"/>
                            </Border.RenderTransform>
                            <Border.Background>
                                <VisualBrush Visual="{Binding ElementName=content, Path=Content}"/>
                            </Border.Background>
                        </Border>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
                                                   Path=Content,
                                                   Converter={StaticResource TypeOfConverter}}"
                                 Value="{x:Type sys:String}">
                        <Setter TargetName="secondContent" Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TypeOfConverter

public class TypeOfConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value == null) ? null : value.GetType();
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
+1

, (TextBlock) . , "", .

, , TextBlock, VisualBrush.

+3

Source: https://habr.com/ru/post/1795076/


All Articles