WPF/XAML  - How to reference a static resource

JElster
JElster used Ask the Experts™
on
Hi..
I have a custom button... the XAML is JUST



<Button x:Class=".BaseTypes.NoteButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Style="{StaticResource DefaultButton}">

   
</Button>


Along with code behind...
I get an error that the static resource cannot be found...

I've tried adding   the Resource Dictionary like this.. but it doesn't work either


<Button x:Class="LandonIP.PWBReader.BaseTypes.NoteButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Style="{StaticResource DefaultButton}">
 
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/BaseTypes;component/StyleResource.xaml" />
            <ResourceDictionary         </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>


</Button>

How can I reference the static resource..? What's wrong with my XAML?
thx

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Navneet.Net Full Stack Developer
Commented:
Hi!

1.Have you added namespace of reference
2. Have you assigned Key To your static refernce

thanks!

Author

Commented:
The above code is all I have..  what do I need to add?
It's just single Button
Navneet.Net Full Stack Developer

Commented:
Hi!

Like this

<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.Resources>
<SolidColorBrush x:Key="BlueBrush" Color="Blue" />
</Window.Resources>

<Grid Background="{StaticResource BlueBrush}">
</Grid>
</Window>


I will check your sources
By that time try this
Thanks!
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Navneet.Net Full Stack Developer

Commented:
HI!

If you are using
Resources Dictionary then

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
<ResourceDictionary Source="Dictionary2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

<Grid Background="{StaticResource BlueBrush}">
</Grid>

In this case the x:key=BlueBrush is in one of the resources you added above

Thanks!

Author

Commented:
I'm just calling... It's just a button not a window... a Window works fine

NewButton btn = New NewButton()  

But it doesn't load the resource


<Button x:Class="LandonIP.PWBReader.BaseTypes.NoteButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           Style="{StaticResource DefaultButton}">
 
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/BaseTypes;component/StyleResource.xaml" />
            <ResourceDictionary         </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>


</Button>

Navneet.Net Full Stack Developer

Commented:
How you are testing the Button?
I mean compile abd Test?
Is it is not the contained within some Window OR Usercontrol?
You can also reference any key in StyleResource.xaml

Author

Commented:
It is contained in a window.... I'm just adding my custom button in code to my window

MyButton btn = new Button...

I'm trying to apply the AERO theme to my button.. but it doesn't work.
MyButton btn = New Button...
This creates an instance of Button, but you then need to add it somewhere in your Window's control hierarchy.
The xaml:
<Button ...
... also creates an instance of a button, not the same one as any you may create in code
A static resource needs to be defined in xaml prior to its first use.

Author

Commented:
In the  XAML  of   MyButton is a Resource Dict but it doesn't work.. What do I need to add to my MyButton XAML see above in my original question

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/BaseTypes;component/StyleResource.xaml" />
            <ResourceDictionary         </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>


<Button ... >
            Anything in here defines the button Content,
            In the xaml you posted the resource dictionary is therefore set as the button content
</Button
As was said in other posts, the definition of a resource needs to be contained in a Resources section, e.g.
<Windows.Resources> add resources here </Windows.Resources>
If you want to restrict the resource to the one button you could try:
<Button>
<Button.Resources>
     ... add you resources here
</Button.Resources>
</Button>

Author

Commented:
Button.Resources did not work...
Add your resource entries to Windows.Resources as has been suggested.
Are you sure that there's a reource in there with the Key 'DefaultButton'?

Author

Commented:
I did.. ye.s...

Here's my button code... looks like Aero in the IDE when I programmatically add it the Aero does not appear.. thanks


NoteButton btn = new NoteButton

<Button x:Class="BaseTypes.NoteButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="400" Height="50"
    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:ui="clr-namespace:System.Windows.Documents;assembly=PresentationUI"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <Button.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
     
                <ResourceDictionary Source="/PresentationFramework.Aero;component/themes/aero.normalcolor.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

   

    </Button.Resources>


</Button>
I really don't understand what you're trying to do.
As I said before, the following code creates a button instance but doesn't add it to the Window
    NoteButton btn = new NoteButton
... so it's not apparent what you expect of it.

The attached code shows the application of a style to a button and how all the pieces relate.

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Style="{StaticResource MyButtonStyle}">
            Hi There!
        </Button>
    </Grid>
</Window>

Open in new window

Author

Commented:
I have the NoteButton.cs and .xaml.
I have a  helper class that adds the notebutton to a form.
When the Note is added the Aero theme is not applied to the NoteButton



        public static Button GenerateANoteButton(ReaderNotes theNoteObject)
        {
                     NoteButton noteButton = new NoteButton(ref theNoteObject);
            try
            {




     ic.Child = Notebook.NoteManagerHelper.GenerateANoteButton(theNoteObject);
                Paragraph ph = new Paragraph() { TextAlignment = TextAlignment.Right };
                ph.Inlines.Add(ic);
However you get your button onto the window, if you want to apply a style to it from a resource then
1. The style needs to be defined in a resource somewhere in the hierarchy before the added button
2. The style needs to be applied to the button instance - that can happen in several ways
   i) The Style can be added directly to the button instance
  ii) The Style property on the Button can be set to point to a named resource
 iii) A unnamed resource can be defined that has a TargetType set to the object in question (in this case it seems not to be the standard WPF button)

<Button ... in xaml is the definition of a new instance of the standard WPF button and can never provide styling to another object.

Author

Commented:
Ok.. That's starting to make sense...  how do I then point the button to named resource in code or apply to the new button instance.     It's just the default Aero

Source="/PresentationFramework.Aero;component/themes/aero.normalcolor.xaml"/>


thanks again

Author

Commented:
Partial

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial