6

次の単純化されたコードが TextBlock の font-size を 50 に設定しないのはなぜですか?

<Window.Resources>
    <ControlTemplate TargetType="ContentControl" x:Key="Test">
        <ContentPresenter TextBlock.FontSize="50" />
    </ControlTemplate>        
</Window.Resources>        
<Grid>
    <ContentControl Template="{StaticResource Test}">
        <TextBlock>Test should be rendered big</TextBlock>
    </ContentControl>                   
</Grid>

FontSize プロパティの値を変更すると、Visual Studio は必要なサイズでテキストを表示します。アプリをコンパイルまたは実行した後、テキストブロックのサイズは常に既定のサイズにリセットされます。

スタイルと埋め込みリソースを使用してさまざまなバージョンもテストしましたが、ContentPresenter を含む ControlTemplate 内から添付された dp を継承するように設定できないという状況で常に終了します。これは設計によるものですか?

4

3 に答える 3

13

この動作の理由を見つけました-それは設計によるものです:

ContentControl のコンテンツが既に WPF 要素である場合は、それを ContenPresenter で使用する前に作成されます。したがって、要素論理的な親はContentControlです。これは、ContentControl マークアップを次のように変更することで確認できます。

<ContentControl Template="{StaticResource Test}" TextBlock.FontSize="50">                
    <TextBlock>
            This text now is shown with a size of 50
    </TextBlock>                    
</ContentControl>

この例では、テキスト サイズは必要に応じて 50 です。Visual Studio の wpf-visualizer でもこの議論を証明できます。親は ContentControl であり、dp 継承により、FontSize は親 (ContentControl) から取得され、テキストは 50 のサイズで表示されます!

ContentControl にコンテンツとしてテキストのみが含まれている場合、別の動作が観察されます。

<Window.Resources>
    <ControlTemplate x:Key="Test"  TargetType="{x:Type ContentControl}">
        <ContentPresenter  TextBlock.FontSize="50"/>
    </ControlTemplate>
</Window.Resources>                
<Grid>
    <ContentControl Template="{StaticResource Test}">                
        This text is shown with a size of 50
    </ContentControl>
</Grid>

このシナリオでは、ビジュアル ツリーにテキストを入力できないため、 TextBoxContentPresenter を通じて作成されます。テキストボックスには親がありませんが、TemplateParent プロパティは TextBoxes 親として ContentPresenter につながり、DP システムは ContentPresenter からの添付依存プロパティの継承を通じて FontSize 値を取得します。そのため、このシナリオではフォント サイズが 50 に変更されています。

ここでは、さまざまなシナリオについて説明します

私が理解していないのは、VS2010 がコンパイル前に FontSize 50 を表示する理由です。

于 2010-06-18T09:55:20.427 に答える
0

どうですか :

<Window.Resources>
    <ControlTemplate TargetType="ContentControl"
                     x:Key="Test">
        <Border TextBlock.FontSize="50">
            <ContentPresenter />
        </Border>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <ContentControl Template="{StaticResource Test}">
        <TextBlock>Test should be rendered big</TextBlock>
    </ContentControl>
</Grid>
于 2010-06-17T15:49:59.027 に答える
0

このようなものが機能するようになったので、それは興味深いです。違いはありますか?

<Style x:Key="SingleWaveItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid Background="{StaticResource WindowBackgroundColor}">
                        <Border Width="125" x:Name="BorderItem" Height="60" Margin="5" BorderThickness="2" ClipToBounds="True" BorderBrush="{StaticResource ViperPanelBorderColor}" Style="{StaticResource ButtonBorderStyle}">
                            <Rectangle x:Name="BackgroundRec" Fill="{StaticResource ViperPanelBorderColor}" Stroke="Transparent" Width="125" Height="60" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ContentPresenter Name="TheContentPresenter" Width="115" Height="60" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="BorderItem" Property="BorderBrush" Value="{StaticResource NavBar_HighlightBrush}"/>
                            <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource NavBar_HighlightBrush}"/>
                            <Setter TargetName="TheContentPresenter" Property="TextElement.Foreground" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



    <DataTemplate x:Key="SingleWaveDataTemplate" DataType="ListBoxItem">
        <StackPanel>
            <StackPanel Orientation="Horizontal">

                <TextBlock FontWeight="Bold" Text="{Binding Name, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <TextBlock FontSize="8" Text="{Binding CreationDate, Mode=OneWay}" Width="{Binding ElementName=this, Path=Content.DesiredWidth}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

xamlページには次のものがあります:

<ListBox Background="Transparent" ItemTemplate="{StaticResource SingleWaveDataTemplate}" ItemContainerStyle="{StaticResource SingleWaveItemContainerStyle}" BorderThickness="0" ItemsSource="{Binding AllModes, Mode=OneWay}" Height="{Binding ElementName=this, Path=Parent.Height}" SelectedItem="{Binding CurrentSingleWaveModeViewModel, Mode=TwoWay}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Height="{Binding ElementName=Parent, Path=Height}" Background="{StaticResource WindowBackgroundColor}"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>

おそらく、目的の効果を得るためにデータ テンプレートを使用する必要があるでしょうか?

于 2010-08-12T20:58:19.853 に答える