私はこの質問Label.Content
を見ていましたが、文字列以外の値へのバインディングは暗黙的なスタイルを適用することを発見しましたTextBlock
が、文字列へのバインディングは適用されません。
問題を再現するためのサンプル コードを次に示します。
<Window.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding SomeString}" Background="Red"/>
<Label Content="{Binding SomeDecimal}" Background="Green"/>
</StackPanel>
</Grid>
バインドされた値のコードの場所
SomeDecimal = 50;
SomeString = SomeDecimal.ToString();
最終結果は次のようになりMargin
ます。暗黙的な TextBlock スタイルのプロパティが、非文字列のみにバインドされた Label に適用されます。
両方のラベルは次のようにレンダリングされます
<Label>
<Border>
<ContentPresenter>
<TextBlock />
</ContentPresenter>
</Border>
</Label>
VisualTree をSnoopでチェックアウトすると、2 番目の TextBlock が暗黙のスタイルから Margin を適用し、最初の TextBlock は適用しないことを除いて、両方の要素がまったく同じに見えることがわかります。
Blend を使用してデフォルトのラベル テンプレートのコピーを取り出しましたが、そこに奇妙なものは見当たりません。テンプレートを両方のラベルに適用すると、同じことが起こります。
<Label.Template>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="True">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Label.Template>
また、デフォルトContentTemplate
を a に設定すると、TextBlock
両方のアイテムが暗黙的なスタイルなしでレンダリングされるため、WPF が文字列以外の値を UI の一部としてレンダリングしようとするときに何か関係があるはずです。
<Window.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="TemplatedStyle" TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding }"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="26"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding SomeString}" Background="Red"/>
<Label Content="{Binding SomeDecimal}" Background="Green"/>
<Label Content="{Binding SomeString}" Background="Red"
Style="{StaticResource TemplatedStyle}"/>
<Label Content="{Binding SomeDecimal}" Background="Green"
Style="{StaticResource TemplatedStyle}"/>
</StackPanel>
</Grid>
UI に挿入された非文字列は暗黙の TextBlock スタイルを使用して描画されるが、UI に挿入された文字列は描画されないロジックは何ですか? そして、これはどこで発生しますか?