473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
7 10877
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
1
by: aspnet20vb_mike | last post by:
I have a Custom Control which inherits from GridView. I call it GridViewSort and it is in a namespace "PDS.WebControls". I add it to the Toolbox, drop it on my WebForm and it shows up and...
2
by: Pon | last post by:
Hi everybody, I posted that on the Avalon ng but it seems there is not so much wpf developpers which use VB. Is there more VB developpers which work with WPF ? I'm trying to do a very simple...
4
by: moondaddy | last post by:
I'm trying to databind a dependency property in a custom control (CenterX) to the Line.X1Property property of line. If I can correctly do this (and the same for the 'Y' props) then when I drag my...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
1
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello All-- Just a general (or perhaps loaded) question - is it considered good or bad practice to use code-behind with a CustomControl.generic.xaml file? For instance, with a control I'm...
3
by: Just_a_fan | last post by:
I have been through two books where they talk about the (almost) parallel control set for WPF and VB. They go through the small difference and talk about everything going on a form but what they...
2
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have an existing (WPF) Class Library project that I'd like to add a WPF Custom Control to. However, when I try to Add New Item, that isn't one of the available options. Perhaps this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.