Automatically freeze wpf objects created from templates

Is it right that wpf objects that are freezed automatically freeze if they are generated using a DataTemplate, ControlTemplate or style?

+3
source share
2 answers

I played around with brushes in VisualTree-Analyzer a bit to see how automatic freezing works. Here is the result of my tests:

All brushes that were attached via ControlTemplates, DataTemplates or styles were frozen, regardless of whether the Brush declaration was frozen in the template or in the style.


To do this, I checked the resulting visual tree of the following templates and styles:

<DataTemplate x:Key="Test_DataTemplate">
    <Grid> 
        <!-- Explicit creation to ensure Brush is !IsFrozen through the converter-->
        <Grid.Background> 
            <SolidColorBrush Color="Red"/>
        </Grid.Background>
        <TextBlock Text="{Binding}"/>
    </Grid>
</DataTemplate>

<ControlTemplate x:Key="Test_ControlTemplate"  TargetType="{x:Type ContentControl}">
    <Border >
        <Border.Background>
            <SolidColorBrush Color="Green" />
        </Border.Background>
        <ContentPresenter />
    </Border>
</ControlTemplate/>

<Style x:Key="Test_Style" TargetType="TextBlock">
    <Setter Property="Background" >
        <Setter.Value>
            <SolidColorBrush Color="Yellow" />                    
        </Setter.Value>                
    </Setter>
</Style>   

<TextBlock >
    <TextBlock.Style>
    <Style TargetType="TextBlock">
        <Setter Property="Background" >
            <Setter.Value>
                <SolidColorBrush Color="Violet" />                    
            </Setter.Value>                
        </Setter>
    </Style>
    </TextBlock.Style>            
</TextBlock>

, .

, , . , " " , WPF , Brush, . . .

, , "". . , . , . - , , .

+1

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


All Articles