/ / LabelのコンテンツでTextBlockを明示的に使用すると、ContentPresenterが--net、wpf、templates、xaml、label

ラベルのコンテンツでTextBlockを明示的に使用すると、ContentPresenterが奇妙に動作します。 - net、wpf、templates、xaml、label

私は習慣がある ControlTemplate ダブル ContentPresenters。テンプレートは、 Label。私が "ランダムタコ"(テキストのみ)をLabelのコンテンツとして使用すると、期待どおりに動作します。私が "<TextBlock>ランダムタコ</ TextBlock>"コンテンツとして、それは働かない(1つのContentPresenterのみが視覚的に表現される)。私は、次のコードを使用して動作を再現します。

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

そして、あなたはそれがどのように見えるかを見ることができます:

http://i56.tinypic.com/1zgdull.png

私は思った Contentプロパティにテキストだけを入力すると、TextBlockでラップされます、なぜ2番目のラベルのビジュアルが正確に最初のものとは異なる表現ですか? 2番目のラベルを正しく動作させる方法(最初のラベルのようにしますが、テンプレートを変更するだけです)ありがとう!

回答:

回答№1は1

違いは...

  • コンテンツを文字列として設定すると Label、a TextBlock それぞれの文字列に対して生成されます ContentPresenter.
  • あなたが TextBlock 直接 Content のために Label、あなたはどこにいるのでしょうか? ContentPresenterしかし、あなたは1つしか持っていないので TextBlock一度に1つの場所にしか置くことができません。

更新

<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();
}
}

回答№2の場合は3

ここでの根本的な問題は、「視覚的な TextBlock)を一度に2つの異なる場所に配置します。ビジュアルには親が1つしかないため、コンテンツプレゼンターの1人が「勝利」し、もう1人がコンテンツを獲得できません。

あなたがしたいのは、 TextBlock それから、 VisualBrush.