Out of bound error in resource dictionary - wpf

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.

Related

Default Title bar appearing even with WindowChrome set

I'm using WindowChrome, and customising the border and the title bar of my application, and it works fine for the most part:
However, for some reason when I run on a certain Virtual Machine (in this case, a German language one using VMware), a white border and title bar (though with no title) appears, and covers my custom title bar:
The odd thing is that I have tried inspecting the application using Snoop, and even on the VM, Snoop does not seem to acknowledge this bar exists. e.g. hovering over the different WPF components, and if I use Snoop's "magnify" function, the title bar appears correctly!
Where could this title bar be coming from and how to get rid of it?
Here is XAML code for a simple application that exhibits the same problem:
<Window x:Class="XamlMessing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XamlMessing"
mc:Ignorable="d"
Title="MainWindow" Height="298" Width="399" Background="#FF590B0B">
<Window.Resources>
<WindowChrome x:Key="MyWindowChrome">
<WindowChrome.CaptionHeight>48</WindowChrome.CaptionHeight>
</WindowChrome>
<Style x:Key="MyWindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="WindowState" Value="Normal" />
<Setter Property="WindowStyle" Value="SingleBorderWindow" />
<Setter Property="WindowChrome.WindowChrome" Value="{StaticResource MyWindowChrome}"/>
</Style>
</Window.Resources>
<Window.Style>
<StaticResource ResourceKey="MyWindowStyle"/>
</Window.Style>
<Grid Margin="0,0,81,107">
<TextBlock HorizontalAlignment="Left" Margin="125,89,0,0" TextWrapping="Wrap" Text="Hello, World" VerticalAlignment="Top" Height="61" Width="175" FontSize="24" Foreground="#FFDAE463"/>
</Grid>
</Window>
OK it seems to be fixed by setting...
<WindowChrome.GlassFrameThickness>0</WindowChrome.GlassFrameThickness>
But I would still be interested to know what was causing it to appear in the first place, since that setting wasn't required on devices other than the VM...

StaticResource reference 'Settings' was not found

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.

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/

How to create StackedBarSeries with custom tooltip without losing standard colors

I have a StackedBarSeries in Silverlight 4 charting (latest release).
I have created a DataPointStyle called MyDataPointStyle for a custom tooltip. By itself this breaks the standard palette used for the different bars.
I've applied a custom palette - as described in David Anson's blog to the chart.
However when I have the DataPointStyle set for my SeriesDefinition objects it does not use this palette.
I'm not sure what I'm missing - but David specifically says :
... it enables the use of
DynamicResource (currently only
supported by the WPF platform) to let
users customize their DataPointStyle
without inadvertently losing the
default/custom Palette colors. (Note:
A very popular request!)
Unfortunately I'm inadvertently losing these colors - and I can't see why?
<chartingToolkit:Chart Title="SKU Sales" x:Name="chartItemSales"
Grid.Column="1">
<chartingToolkit:Chart.Palette>
<dataviz:ResourceDictionaryCollection>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="Blue"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
</dataviz:ResourceDictionaryCollection>
</chartingToolkit:Chart.Palette>
<chartingToolkit:Chart.Series>
<chartingToolkit:StackedBarSeries>
<chartingToolkit:SeriesDefinition
IndependentValueBinding="{Binding SKU}"
DependentValueBinding="{Binding Qty}"
DataPointStyle="{StaticResource MyDataPointStyle}"
Title="Regular"/>
<chartingToolkit:SeriesDefinition
IndependentValueBinding="{Binding SKU}"
DependentValueBinding="{Binding Qty}"
DataPointStyle="{StaticResource MyDataPointStyle}"
Title="FSP Orders"/>
<chartingToolkit:StackedBarSeries.IndependentAxis>
<chartingToolkit:CategoryAxis Title="SKU" Orientation="Y" FontStyle="Italic" AxisLabelStyle="{StaticResource LeftAxisStyle}"/>
</chartingToolkit:StackedBarSeries.IndependentAxis>
<chartingToolkit:StackedBarSeries.DependentAxis>
<chartingToolkit:LinearAxis Orientation="X" ExtendRangeToOrigin="True" Minimum="0" ShowGridLines="True" />
</chartingToolkit:StackedBarSeries.DependentAxis>
</chartingToolkit:StackedBarSeries >
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>
The clue is in the quote you posted from David "currently only supported by the WPF platform" that is, its not supported on Silverlight.
As soon as you supply your own DataPointStyle you replace any style that would have been supplied by the Palette (either the default one or your custom palette).
Edit:
Here is how its done. Instead of supplying a style to the DataPointStyle property of a series or definition you leave it to the pallete. However the Styles in the palette can use the Style object's BasedOn property to avoid duplication. So:-
<UserControl.Resources>
<Style x:Key="MyDataPointStyle" TargetType="DataPoint">
<!-- Set up the general style for the points may even include a Template -->
</Style>
...
<chartingToolkit:Chart.Palette>
<dataviz:ResourceDictionaryCollection>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="chartingToolkit:BarDataPoint" BasedOn="{StaticResource MyDataPointStyle}" >
<Setter Property="Background" Value="Blue"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="chartingToolkit:BarDataPoint" BasedOn="{StaticResource MyDataPointStyle}">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="chartingToolkit:BarDataPoint" BasedOn="{StaticResource MyDataPointStyle}">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
</dataviz:ResourceDictionaryCollection>
</chartingToolkit:Chart.Palette>
<chartingToolkit:Chart.Series>
<chartingToolkit:StackedBarSeries>
<chartingToolkit:SeriesDefinition
IndependentValueBinding="{Binding SKU}"
DependentValueBinding="{Binding Qty}"
Title="Regular"/>
...

Resources