A191919
A191919

Reputation: 3442

WPF xaml source path

I am trying to replace C:\Users\StrangeUser\documents\visual studio 2012\Projects\WPFGO\WPFGO\Resources\New.png with {x:Static icons:New.png} but it does not work New does not exists int namespace clr-namespace:WPFGO.Resources. How to fix it?

xmlns:icons="clr-namespace:WPFGO.Resources"

<UserControl.Resources>
        <Style x:Key="CreateIconButton">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <!--<Image Source="{x:Static icons:New.png}"></Image>-->
                        <Image Source="C:\Users\StrangeUser\documents\visual studio 2012\Projects\WPFGO\WPFGO\Resources\New.png" Height="15" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

Upvotes: 1

Views: 1292

Answers (2)

Steve
Steve

Reputation: 11963

Add this to a top level Resource tag

<BitmapImage x:Key="somekey" UriSource="..." />

and then you will be able to do

<Image Source="{StaticResource somekey}"/>

Upvotes: 2

BolletuH
BolletuH

Reputation: 124

I ran into a similar problem like this, and wanted an easier way to manage these resources. Assuming you are going to control the file directory hierarchy from outside VS you can use the properties file to your advantage.

  1. In VS go to Properties -> Resources in your project file.
  2. You will need to assign a Name value (what you will be referencing) e.g. myResource, the actual location of the file in question, and a comment if necessary.
  3. Go ahead and save the file, and rebuild the project to synchronize the properties with XAML.

Now you have the ability to reference properties in your project such as this:

xmlns:prop="clr-namespace:WPFGO.Properties"

<Image Source="{Binding Source={x:Static prop:Resources.myResource}}"/>

The neat part about this would be obtaining the ability to change the location in just the resources file. XAML will build the Uri for you as well making it just a little more simple for you as the developer.

Update:

It looks like you have added the resource to VS (WPFGO\Resources\New.png) under the Resources folder. The value in the properties section might look something like this:

/Resources/New.png

Upvotes: 0

Related Questions