1

I believe my question is fairly simple and yet I am having difficulty implementing it successfully. I simply wish to extract the styling of elements in my WPF application because the xaml is rather crowded and xaml is often duplicated.

I therefore wish to place the styling in an external xaml file, in the form of a resource dictionary, then reference that file in the resources section of my code.

I have the following .xaml file:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="PTextBox" TargetType="TextBox" x:Name="PTextBox">
    <Setter Property="Foreground" Value="#FFA1C8E7"/>
    <Setter Property="BorderBrush" Value="#FFA1C8E7"/>
</Style>

And I reference the dictionary here:

<UserControl.Resources>
    <ResourceDictionary x:Key="PegasusStyles">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../../Resources/Styles/PegasusStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

Visual studio has resolved the file location so I know this reference is correct.

The text box the styles are applied to then references the style:

<TextBox Style="{StaticResource PTextBox}"/>

If left as a static resource I get a xaml parse error like so:

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

And if I make the resource dynamic then the styles simply do not get applied at runtime.

I'm not sure if xaml files require certain properties before run time but mine are as follows:

enter image description here

If someone could answer this mystery it would be wonderful. I googled till my fingers bled but none of the answers posted by others have resolved my issues and this seems very rudimentary.

EDIT: Solved. Switching the build action to Page instead of resource has fixed my issue as suggested by Andrew Stephens. This had been hidden by another underlying problem, which is that I had added a boolean to visibility converter (common tool) to my resources. This alone is fine but once I had declared a resource dictionary this converter needed to be brought inside the dictionary as well.

5
  • 2
    The Build Action of your .xaml resource file should be "Page" rather than "Resource". Commented Sep 21, 2017 at 13:38
  • Thank you for your reply. I have updated the build action to "Page" however the parsing error still occurs. Commented Sep 21, 2017 at 14:02
  • It sounds like a XAML syntax error somewhere, but can also be caused by an unhandled exception in the main window code-behind (if you have any code in here). There are a few ways to debug this cryptic exception: geekswithblogs.net/lbugnion/archive/2007/03/14/108728.aspx (read the comments for more tips) Commented Sep 21, 2017 at 14:23
  • Thank you for pointing me towards this site as I am sure it will come in handy in the future. Unfortunately (I suppose) there is no error in the code behind. It initialises successfully, then when the tool window I am developing begins to open the xaml error is thrown. Commented Sep 22, 2017 at 8:41
  • 1
    Thank you Andrew it seems your solution did fix my issues it was just hidden behind MORE issues. Please add your comment as a full answer and I will mark it as the preferred answer. Commented Sep 22, 2017 at 10:07

2 Answers 2

1

It sounds like a XAML syntax error somewhere, but can also be caused by an unhandled exception in the main window code-behind (if you have any code in here). There are a few ways to debug this cryptic exception here (read the comments for more tips)

Also the Build Action of your .xaml resource file should be "Page" rather than "Resource".

Sign up to request clarification or add additional context in comments.

Comments

0

Try building the solution with your newly merged dictionary before you start referencing the external styles in your xaml.

It may seem counter intuitive but it is possible for visual studio to know about a type in another xaml file without the designer being aware which can cause bugs like this.

Koda

1 Comment

I'm not certain I understand. I have removed the reference to the style on the ui element itself but kept the resource dictionary reference in there. It now builds and runs successfully but without the styles of course. However once I add the style reference and run it again it fails. This points to a xaml error in the external file but it would appear flawless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.