Why should I use Resources in WPF?

I just dive into WPF and find this a bit strange and disappointing in my style: why the value of something can be a resource, but you cannot set the value directly in what Resource represents? Example:

It's really:

<ToggleButton>
    <ToggleButton.Resources>
        <Image x:Key="cancelImage" Source="cancel.png" />
    </ToggleButton.Resources>
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Content" Value="{DynamicResource cancelImage}" />
        </Style>
    </ToggleButton.Style>
</ToggleButton>

But this is not so:

<ToggleButton>
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Content">
                <Setter.Value>
                    <Image Source="cancel.png" />
                </Setter.Value>
            </Setter>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

What is the difference? Why are both not working? I don’t like creating a Resource for some things, because it shares my code and can make it difficult to read.

And yes, I know, my example can be simplified, like this

<ToggleButton>
    <Image Source="cancel.png" />
</ToggleButton>

but that is not the point.

+5
source share
1 answer

"". ( ) , Freezable.

, "" ( ResourceDictionary), .

+3

All Articles