StaticResource reference 'Settings' was not found - wpf

A WPF application of mine has started throwing this error in the designer view for a window. It compiles and runs without issue, but will not load in designer.
The strangest part of the error, however, is that it only occurs on the first reference to settings. In the code below, if I comment out the first <Setter>, the error moves down to the next one. If I then uncomment that first <Setter>, the error moves back to it.
<Style TargetType="{x:Type ComboBox}">
<Setter Property="FontSize" Value="{Binding Source={StaticResource Settings}, Path=Default.setFontSize}" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{Binding Source={StaticResource Settings}, Path=Default.setFontSize}" />
</Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="{Binding Source={StaticResource Settings}, Path=Default.setFontColor}" />
</Style>
Any ideas?

I just came across the same issue. Here is how I solved it globally for the app in the App.xaml. Notice xmlns:properies and properties:Settings lines.
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:properties="clr-namespace:MyApp.Properties"
StartupUri="MainWindow.xaml">
<Application.Resources>
<properties:Settings x:Key="Settings" />
</Application.Resources>

Hmm - that is very strange.. in your Xaml file, do you see any elements with the Settings keys?
maybe something is injecting it into the resource dictionary when it runs so that's why it builds and runs fine.

Related

App.xaml exception. Provide value on 'System.Windows.Markup.StaticResource' threw an exception

I've fund a few other things on this, but none with answers that help. In my App.xaml file I have a load of styles within a resource dictionary. When I run it on a windows 7 machine, it works absolutely perfectly, but when I run it on an XP machine, it crashes with the exception above. Wonder if anyone could help why?
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="{Binding Source={x:Static my:Settings.Default}, Path=DefaultFontSize}"/>
<Setter Property="FontFamily" Value="{Binding Source={x:Static my:Settings.Default}, Path=DefaultFontFamily}"/>
</Style>
Also in this file I have a series of ResourceDictionary.MergedDictionaries, but the issue seems to be with x:Static my:Settings.Default.
Thanks

Out of bound error in resource dictionary

I have put my shared styles in a single resource dictionary which I include in the application. For example, in a file called GlobalStyles.xaml I have the following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="BaseStyle" TargetType="FrameworkElement">...</Style>
<Style x:Key="ExtendedStyle" TargetType="FrameworkElement" BasedOn="{StaticResource BaseStyle}">...</Style>
</ResourceDictionary>
I include this file in the app.xaml as follows:
<Application.Resources>
<ResourceDictionary Source="GlobalStyles.xaml" />
</Application.Resources>
This should work but I am getting the following build error for the ExtendedStyle: Error: Index was outside the bounds of the array.. If I remove the BasedOn directive I get the same error for BaseStyle but none for ExtendedStyle. This is really wierd. Can anybody explain this error?
EDIT:
The styles are:
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<!-- Background -->
<Setter Property="Control.Background" Value="{StaticResource White0ColorBrush}"/>
<!-- Border -->
<Setter Property="Control.BorderBrush" Value="{StaticResource Green1ColorBrush}"/>
<Setter Property="Control.BorderThickness" Value="0"/>
<!-- Font -->
<Setter Property="Control.FontWeight" Value="Light"/>
<Setter Property="Control.Foreground" Value="{StaticResource Green0ColorBrush}"/>
</Style>
and
<Style x:Key="ExtendedStyle" TargetType="FrameworkElement" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Control.Background" Value="{StaticResource Green1ColorBrush}"/>
<Setter Property="Control.Foreground" Value="{StaticResource White0ColorBrush}"/>
</Style>
I don't think there is anything special about these two styles. On restarting VS, I am getting the error for another style dependent on ExtendedStyle.
EDIT 2:
I created a new empty WPF Application project and included the GlobalStyles.xaml file in the app.xml. That built fine. Then I added just these lines to the window.xaml and got the same error:
<Grid>
<Border BorderThickness="2" CornerRadius="5" BorderBrush="{StaticResource White0ColorBrush}" Style="{StaticResource ExtendedBaseStyle}">
<Grid>
<TextBox x:Name="SearchField" Height="20" BorderThickness="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
</Grid>
</Border>
</Grid>
It has to be a bug with VS2012 which I am using. I moved to VS2010 and the problem went away. I have had numerous problems with VS2012 designer. It hangs frequently, throws exceptions, complains about syntax errors when there are none and now this. I will stick to VS2010 for now.

How to set a ResourceDictionary to a ContentControl.Content?

In my application, I have a ContentControl and this shows the Content property using DataTemplates.
Right now, I need to pass this DictionaryResource to the content property. So I did this (I'm not sure)
<ContentControl Content="{Binding CurrentViewModel">
<ContentControl.Resources>
<ResourceDictionary Source="/MathematicsBusiness.Infrastructure;component/Resources/ThemeResources.xaml" />
</ContentControl.Resources>
</ContentControl>
And this contain my dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="40" />
<Setter Property="FontFamily" Value="Georgia" />
</Style>
<!--<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="40" />
<Setter Property="FontFamily" Value="Georgia" />
</Style>-->
</ResourceDictionary>
And it works, all the data templates show the textblocks with that style. But if I use the commented style, it does not work. It throws me an error:
Cannot find a Resource with the Name/Key TextBlockStyle
Why is happening that? If the style does not have a Key, it works. But if I set a key, it doesn't work.
If you specify a key without using it, it should not be a problem. My guess is that you are calling TextBlockStyle somewhere else (maybe your visual state or your code).
The error you are having usually happens if you are trying to use the key, but you haven't specified it in your xaml.
Other possible cause is that you are trying to use the key outisde the ContentControl.

WPF default themes and custom styles not working together

Hey, I have a WPF application targeted at XP machines. The problem is that we wish to run with a WPF XP luna theme rather than classic and most of our clients run in classic mode. Our clients are all internal, it's just that their machines were configured with XP classic.
In theory, this is as simple as adding this to the application:
<ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />
In practice, as soon as a touch any of the styles (say add a margin to TextBox), they styles seem to revert back to classic theme.
This displays correctly (Style Luna):
<TextBox Width="80" Height="20" />
This displays correctly (Style Luna):
<TextBox Width="80" Height="20" Background="Brown">
This displays incorrectly (Style Classic), note it does not matter now many nodes there are in the style block - zero is enough to confuse things:
<TextBox.Style><Style></Style></TextBox.Style></TextBox>
Long and short, overriding the default OS theme seems to preclude further use of styles. What am I missing here?
See the select answer for 80% of the story. The full story is this: I must supply the ‘BasedOn’ setting as well. Unfortunately, this means we can not override, say textbox, without causing a loop. Defining:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="0,2,0,2" />
:
</Style>
would result in the error: "a loop was detected in the property expression".
The way I chose to get around this was to force named styles everywhere. For example:
<Style x:Key="TextBase" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="0,2,0,2" />
:
</Style>
<Style x:Key="Text25Chars" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBase}">
<Setter Property="Margin" Value="0,2,0,2" />
:
</Style>
try this:
<TextBox>
<TextBox.Style>
<Style BasedOn="{StaticResource {x:Type TextBox}}">
</Style>
</TextBox.Style>
</TextBox>
edit:
forgot the TargetType, this works for me:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />
</Window.Resources>
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Blue" />
</Style>
</TextBox.Style>
tototototottototo
</TextBox>
</Window>

Setting an Elements.Resource Style using BasedOn within a Resource Dictionary

I have a Resource Dictionary that I am using to define the look-and-feel (style) for my application.
I have just created another Resource Dictionary that contains DataTemplates that I am using on several different screens (and even multiple times within the same screen) to display my business objects.
I would like to change some of the default styles within my DataTemplates so that the controls fit better; however I would like the controls to inherit the same style as the rest of the screen. So, naturally I want to use the BasedOn property for this task.
The problem that I am having is that I'm not sure what to set the BasedOn property to.
For example, in the resource dictionary that contains my styles (called "myStyle.xaml") I have:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="#F5F5F5" />
<Setter Property="FontSize" Value="12"></Setter>
<Setter Property="Width" Value="120"></Setter>
<Setter Property="FontFamily" Value="Arial"></Setter>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="12"></Setter>
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Background" Value="Black"></Setter>
</Style>
<!-- .... and so on .... -->
</ResourceDictionary>
I am using this resource in the following window:
<Window x:Class="SiteSetupWindow4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
Title="A Screen">
<Window.Resources>
<ResourceDictionary x:Key="defaultStyleX">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="DefaultStyles" Source="Resources/myStyle.xaml" />
<ResourceDictionary x:Name="Templates" Source="Resources/myTemplates.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Now, I have another Resource Dictionary that contains DataTemplates that I am using within my window. It is called "myTemplates". The style is applied to the DataTemplate as expected; however, I would like to overwrite some aspects of the style within the DataTemplate (Like width for example).
This is what I have tired, however I cannot get the BasedOn property to work...
(myTemplate.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="PanelInfo">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Width" Value="120" />
</Style>
<Style TargetType="Label">
<Setter Property="Width" Value="180" />
</Style>
<Style TargetType="ComboBox">
<Setter Property="Width" Value="120" />
</Style>
<StackPanel.Resources>
<StackPanel Orientation="Horizontal">
<Label Content="Type:"></Label>
<ComboBox>
<ComboBoxItem Content="{Binding Path=Type}" IsSelected="True"></ComboBoxItem>
</ComboBox>
<!--...and so on -->
</StackPanel>
</StackPanel>
</ResourceDictionary>
This fails....I have also tried using DynamicResource, but this also fails.
I'm not sure how to get around this.
Any advise would be greatly appreciated!
Thanks,
-Frinny
I was having the same problem with an extended Button Style.
The ResourceKey= is what solved it for me.
This worked:
<Style x:Name="ButtonVisibility"
TargetType="{x:Type Button}"
BasedOn="{StaticResource ResourceKey={x:Type Button}}">
The way you have BasedOn for a type is correct. This will work in theory as long as, at run time, the style that you are basing it on is merged into the tree correctly. Make sure you have the "myStyles.xaml" merged in correctly. You can check this by removing your style you tried to modify and make sure it displays correctly from your style in "myStyles.xaml."
If it isn't there are a lot of places you can go wrong, but it always helps to try merging the styles in the file you are working on, then work up the tree to see where it's missing.
This utility will help you look at what is happing in the tree at run time.
http://blois.us/Snoop/

Resources