Avatar of JElster
JElsterFlag for United States of America

asked on 

WPF/XAML - How to reference a static resource

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

C#

Avatar of undefined
Last Comment
JElster
SOLUTION
Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of JElster
JElster
Flag of United States of America image

ASKER

The above code is all I have..  what do I need to add?
It's just single Button
Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

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!
Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

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!
Avatar of JElster
JElster
Flag of United States of America image

ASKER

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>

Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

How you are testing the Button?
I mean compile abd Test?
Is it is not the contained within some Window OR Usercontrol?
Avatar of emrahbudur
emrahbudur

You can also reference any key in StyleResource.xaml
Avatar of JElster
JElster
Flag of United States of America image

ASKER

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.
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of JElster
JElster
Flag of United States of America image

ASKER

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>


SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of JElster
JElster
Flag of United States of America image

ASKER

Button.Resources did not work...
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

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'?
Avatar of JElster
JElster
Flag of United States of America image

ASKER

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>
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of JElster
JElster
Flag of United States of America image

ASKER

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);
ASKER CERTIFIED SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of JElster
JElster
Flag of United States of America image

ASKER

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
Avatar of JElster
JElster
Flag of United States of America image

ASKER

Partial
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo