By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

RE: WPF: Custom control and resorce files in a different assembly.

P: n/a
Hi George,

I have downloaded your sample solution and built it on my machine. I got a
compilation error indicating that the type of "CustomResources" doesn't
exist in the following xaml code:

<Expander Style="{DynamicResource {ComponentResourceKey
TypeInTargetAssembly={x:Type res:CustomResources},
ResourceId=CustomExpanderStyle1}}" >

I modify "CustomResources" to "TestExpander" and then pass the compilation.
I add the custom control in the Grid within the Window1. When I run the WPF
application, I find that the custom style isn't applied to the custom
control at all.

After doing some research, I find the reason of the problem is that the
custom control couldn't find the specified style at all. To solve this
problem, I make the following modifications and the custom control can find
the custom style now.

1. Modify the value of the Source property of the ResourceDictionary
element in the Generic.xaml:
<ResourceDictionary ...>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source = "Expander.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

2. Move the Generic.xaml and Expander.xaml files from the folder "Themes"
to the folder "Controls".

3. Merge the resources in the Generic.xaml into the resources of the
TestExpander:
<UserControl x:Class="CustomControls.Controls.TestExpander" ...>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
....
<Expander>
.....
</Expander>
<Expander>
....
<Expander>
....
</UserControl>

I will send the modified sample solution to your email box. Please run it
on your machine to see if there's any problem.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 8 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi George,

Thank you for your prompt response and sample projects!

After doing more research on this issue, I found the reason why the WPF
application couldn't access the resource defined in the custom control
library project now.

Firstly, we need to define the resource that will be accessed by a resource
key in the Generic.xaml file and this xaml file should reside under the
Themes folder.

Secondly, in your custom control library project, the style for the
Expander control uses some other custom styles. Since the style for the
Expander control has a resource key, other custom styles it uses should
also have a resource key respectively.

I have modified the sample project. On the Window1, I place a TestExpander
and a ExpanderControl and a Expander. For the Expander control, I access
the style for the Expander control from the custom control library and set
it to the Style property of the Expander control. It works well on my side.

I will send the modified sample project to your email address. Please run
it on your machine to see if there's any problem.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 9 '08 #2

P: n/a
Thanks Linda,

That's good progress. There's just one missing piece which was one of my
original problems. What you have works good, but the style is located in
the Generic.xaml file. Assuming I add more controls to this assembly, I
will want the styles and templates for each control in its own dictionary
file and then I will want to merge these into the Generic.xaml dictionary.
Supposedly this should work, but I'm getting an error msg:

Error 1 Value
'CustomControls.Controls;component\Themes/ResourceCustomExpander.xaml'
cannot be assigned to property 'Source'. Value does not fall within the
expected range. D:\nwis\Apps\WPF\CustomExpanderControl\From Linda
2\CustomExpanderControl1\CustomExpanderControl\Tes tHarnes\CustomExpander\Themes\Generic.xaml
9 29 CustomControls

I found a number of other people having the same problem and they resoled it
with similar code as below (this is how the syntax should be to merge a
resource dictionary into the Generic.xaml file):

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="CustomControls.Controls;component\Themes/ResourceCustomExpander.xaml"
/>
</ResourceDictionary.MergedDictionaries>

Here's some links that discuss it.

http://sharpinsights.wordpress.com/category/xaml/
http://www.cnblogs.com/Files/sheva/S...ourcesDemo.zip
http://arbel.net/blog/archive/2006/1...ows-theme.aspx
1) Try this: Create a new dictionary file called:
ResourceCustomExpander.xaml
2) copy the code you have from Generic.xaml into this new file.
3) Then merge 'ResourceCustomExpander.xaml' into 'Generic.xaml'.

This code should work for the merge but doesn't:

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="CustomControls.Controls;component\Themes/ResourceCustomExpander.xaml"
/>
</ResourceDictionary.MergedDictionaries>

Please let me know what you find out.

Thanks.
"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:NT**************@TK2MSFTNGHUB02.phx.gbl...
Hi George,

Thank you for your prompt response and sample projects!

After doing more research on this issue, I found the reason why the WPF
application couldn't access the resource defined in the custom control
library project now.

Firstly, we need to define the resource that will be accessed by a
resource
key in the Generic.xaml file and this xaml file should reside under the
Themes folder.

Secondly, in your custom control library project, the style for the
Expander control uses some other custom styles. Since the style for the
Expander control has a resource key, other custom styles it uses should
also have a resource key respectively.

I have modified the sample project. On the Window1, I place a TestExpander
and a ExpanderControl and a Expander. For the Expander control, I access
the style for the Expander control from the custom control library and set
it to the Style property of the Expander control. It works well on my
side.

I will send the modified sample project to your email address. Please run
it on your machine to see if there's any problem.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 10 '08 #3

P: n/a
Hi George,

Thank you for your prompt response!

Modify the code that merges ResourceCustomExpander.xaml into the
Generic.xaml as follows:

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CustomControls;component/Themes/ResourceCustomExpander.xaml"/>
</ResourceDictionary.MergedDictionaries>

Note that you need to set the 'Build Action' property of both the
Generic.xaml and the ResourceCustomExpander.xaml to Page.

I will send the modified sample project to your email address. Please run
it on your machine to see if there's any problem.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 10 '08 #4

P: n/a
Thanks Linda,

It looks good now, but when I run it, the 'ExpanderControl' does not expand.
I put 2 controls in it (2 labels and a checkbox) which it will not show.
any idea why this is happening?

"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:mJ**************@TK2MSFTNGHUB02.phx.gbl...
Hi George,

Thank you for your prompt response!

Modify the code that merges ResourceCustomExpander.xaml into the
Generic.xaml as follows:

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CustomControls;component/Themes/ResourceCustomExpander.xaml"/>
</ResourceDictionary.MergedDictionaries>

Note that you need to set the 'Build Action' property of both the
Generic.xaml and the ResourceCustomExpander.xaml to Page.

I will send the modified sample project to your email address. Please run
it on your machine to see if there's any problem.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 10 '08 #5

P: n/a
Hi George,

Thank you for your quick response!
but when I run it, the 'ExpanderControl' does not expand. I put 2
controls in it (2 labels and a checkbox) which it will not show.

I performed a test putting some controls in the ExpanderControl within the
Window1.xaml and saw the ExpanderControl can expand as expected when I run
the application.

The following is the content of the Window1.xaml in my test:

<Window x:Class="TestHarnes.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:controllib="clr-namespace:CustomControls.Controls;assembly=CustomC ontr
ols"
Title="Window1" Height="361" Width="345">

<StackPanel>
<controllib:TestExpander/>

<Expander Header="this is Expander" Style="{DynamicResource
{ComponentResourceKey TypeInTargetAssembly={x:Type
controllib:TestExpander}, ResourceId=CustomExpanderStyle}}">
<Label>label in expander</Label>
</Expander>

<controllib:ExpanderControl Header="this is ExpanderControl">
<StackPanel>
<Label>label 1</Label>
<Label>label 2</Label>
</StackPanel>
</controllib:ExpanderControl>
</StackPanel>

</Window>

Please try it in the modified sample project I sent you to see if the
problem still exists.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 11 '08 #6

P: n/a
Linda,

Thanks for all the help on this. all is good now!

Have a great weekend!
"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:Sj****************@TK2MSFTNGHUB02.phx.gbl...
Hi George,

Thank you for your quick response!
>but when I run it, the 'ExpanderControl' does not expand. I put 2
controls in it (2 labels and a checkbox) which it will not show.

I performed a test putting some controls in the ExpanderControl within the
Window1.xaml and saw the ExpanderControl can expand as expected when I run
the application.

The following is the content of the Window1.xaml in my test:

<Window x:Class="TestHarnes.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:controllib="clr-namespace:CustomControls.Controls;assembly=CustomC ontr
ols"
Title="Window1" Height="361" Width="345">

<StackPanel>
<controllib:TestExpander/>

<Expander Header="this is Expander" Style="{DynamicResource
{ComponentResourceKey TypeInTargetAssembly={x:Type
controllib:TestExpander}, ResourceId=CustomExpanderStyle}}">
<Label>label in expander</Label>
</Expander>

<controllib:ExpanderControl Header="this is ExpanderControl">
<StackPanel>
<Label>label 1</Label>
<Label>label 2</Label>
</StackPanel>
</controllib:ExpanderControl>
</StackPanel>

</Window>

Please try it in the modified sample project I sent you to see if the
problem still exists.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.

Jul 11 '08 #7

P: n/a
You're welcome, George!

If you have any other question in the future,please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a nice weekend too.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 11 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.