1

I am fairly new to WPF. I understand the concept of defining global application resources to which I can refer throughout the application. I see I can define a textblock under application resources but can't seem to see how to refer to it within a window.

In the Application.Resources I have the following code:

<TextBlock x:Key="ABC_Copyright" Background="Beige" Text="Copyright 2016 ABC Company" />

How to I construct a new textblock in any given window that refers back to the "ABC_Copyright" application resource?

Thanks in advance.

2 Answers 2

1

We define it as a style;

<Style x:Key="ABC_Copyright" TargetType="TextBlock">
   <Setter Property="Background" Value="Beige"/>
   <Setter Property="Text" Value="Copyright 2016 ABC Company"/>
</Style>

Then we use it at whatever instance we need to;

<TextBlock Style="{StaticResource ABC_Copyright}"/>

Hope this helps, cheers.

Sign up to request clarification or add additional context in comments.

1 Comment

No prob, glad you got a remedy.
0

Create a ResourceDictionary where you want to store and just put your your Resource Dictionary name as the source of ResourceDictionary in the App.xaml

           <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Your Resource Dictionary Name"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
         </Application.Resources>

Now in ResourceDictionarystyle your textbolckthat can be accessible from any other palces like that,

<Style x:Key="TxtStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Text" Value="Copyright 2016 ABC Company"/>
    </Style>

Now refer your textblock to the styled textblock

<TextBlock x:Key="ABC_Copyright" Background="Beige" Style="{StaticResource TxtStyle}"/>

1 Comment

If you'd had just moved his x:Key to the style and used it as the resource you'd have got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.