Location>code7788 >text

WPF: Static and Dynamic Resources and Resource Dictionary

Popularity:982 ℃/2024-08-20 22:09:42

WPF: Static and Dynamic Resources and Resource Dictionary

Static versus dynamic resources

We often use styles or control templates placed in the middle, like this:

image-20240820214414564

Static and dynamic resources are used as follows:

<>
    <SolidColorBrush x:Key="SolidColor" Color="#FF0000" />
</>
<Grid>
    <StackPanel>
        <Button Height="40" Margin="10" Content="Button1" Click="Button_Click" />
        <Button Height="40"
                Margin="10"
                Content="Button2"
                BorderBrush="{StaticResource SolidColor}"
                BorderThickness="4" />
        <Button Height="40"
                Margin="10"
                Content="Button3"
                BorderBrush="{DynamicResource SolidColor}"
                BorderThickness="4" />
    </StackPanel>

</Grid>

Difference: Dynamic resources can be changed in the interface according to the instructions, static does not.

image-20240820214834842

image-20240820214859186

resource dictionary

When the style more, this time we need to create a separate resource to manage him, this time we have a resource dictionary:

image-20240820215113473

Port the code in:

image-20240820215241649

This time it is also necessary to load this resource dictionary at load time, in the middle of loading the

image-20240820215351454

The corresponding resource can also be found in main as follows:

image-20240820215507890

The results are still available:

image-20240820215717618