1

I made a ResourceDictionary in a WPF User Control Assembly. I Want to be able to use this across this UserControl and have all the styles in this separated file.

The ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    
    <Style x:Key="c1BtnX1">
        <Setter Property="Background" Value="Bisque"></Setter>
    </Style>    
</ResourceDictionary>

It's Address is The User Control Assembly Resources/mainResX.xaml and the View is in the same assembly/Views/view.xaml

The usage I think could be:

<Border Style="{StaticResource ResourceKey=c1BtnX1}" 
        BorderBrush="Black"  
        Width="20" 
        Height="20">
               <TextBlock Text="X" />
</Border>

Also I tried the below code inside the UserControl, to define Per Control Resources but this way also seems it couldn't find the resources.

 <UserControl ... >
    <UserControl.Resources>
        <ResourceDictionary Source="../Resources/mainResX.xaml" />            
    </UserControl.Resources>

Where and How should I place/Define this ?

Noam M
  • 3,156
  • 5
  • 26
  • 41
LastBye
  • 1,143
  • 4
  • 19
  • 37
  • Is the XAML file's Build action set to Resource? (is it resource? I don't have my coding computer nearby...) – Joe Nov 03 '12 at 15:36
  • It's Build Action was none, I changed it to "Embeded Resource", but I think that would be good if I wanted to use it on cross-assemblies. Not sure on this may be "Content" or ... ?! – LastBye Nov 03 '12 at 16:04
  • If you created it through the Wizard (Resource Dictionary), its setting should be right ad you shouldn't change it. – Joe Nov 03 '12 at 16:16
  • I'm using VS11, Which wizard ? I just clicked on the module and added a ResourceDictionary in the "Resources" folder. – LastBye Nov 03 '12 at 16:22
  • I usually right-click on the project and select "Add -> New Item", then you have a dialog (not really a wiz, sorry) where you can select resource dictionary. Take a look at this (read more than just the answer) http://stackoverflow.com/questions/347614/wpf-image-resources – Joe Nov 03 '12 at 17:33

2 Answers2

4

I can't tell what your file structure is from info provided.

If the resource.xaml and control.xaml are in the same folder of the same assembly, you would just reference mainResX.xaml without "/Resources" first; otherwise you need to account for the file structure somehow.

Are they in the same assembly? You can 'walk' the tree up with as many "../" strings prepended to the location as needed, and the in using the folders (ie, "../Resources/mainResX.xaml")

If they are in different assemblies, you need to specify a pack uri. You can actually always do this, although it is a little cumbersome when not necessary. Here is an example

<ResourceDictionary Source="pack://application:,,,/MyAssembly.Wpf;component/Resources/mainResX.xaml" />

HTH,
Berryl

Berryl
  • 12,471
  • 22
  • 98
  • 182
  • Same Assembly, The View.Xaml is in the Views Folder. and the Resource is in the Resources folder. – LastBye Nov 03 '12 at 15:53
  • Friend before my post I also tried "../Resources/mainResX.xaml" but still seems the UserControl View can't find that. Also I'll try what you provided to give a more detailed info of any further inner problems. – LastBye Nov 03 '12 at 15:59
2

Personally I like to use my App.xaml to specify a "MergedDirectory" of XAML files containing styles that I use globally in my app. I usually have a "DefaultStyles.xaml" to set any global style (like when you want all textboxes in the app to look the same without specifying a style). Then I have a "Styles.xaml" to set specific styles, or you could even have one xaml per control type if you really have a bunch of them...

The fact that you place these under app.xaml makes them global to your app and don't require you to constantly re-specify the paths and dictionaries. Of course this may not fit all coding situations, but for me it's a time saver.

Joe
  • 2,496
  • 1
  • 22
  • 30
  • Thanks for the info, this application may be a bit different from yours, but it's good for to know your idea, +1 and thanks. – LastBye Nov 03 '12 at 15:54
  • Eager to know how do you address some of your Resources while they were in other assemblies based on your method ? – LastBye Nov 03 '12 at 16:02
  • It's the same method as Berryl mentioned in his answer. I'm not sure why it's not working on your end, but again, I don't have access to my coding computer at the moment. – Joe Nov 03 '12 at 16:18