I'm getting a visual studio exception at run time and I have no idea where it's coming from!
I am creating a recipe book and meal planner desktop app in WPF. I'm currently working on the interface that allows users to input ingredients into a recipe.
I have a user control for each of the individual Ingredients, which I dynamically add to a stack panel in a Recipe user control. The stack panel is wrapped in a ScrollViewer using the code below:
<ScrollViewer Grid.Row="3" VerticalScrollBarVisibility="Auto" Margin="20,10,0,0">
<StackPanel Name="IngredientsPanel">
<!--IngredientUserControl objects are dynamically added here-->
</StackPanel>
</ScrollViewer>
Adding ingredients works until the ingredients fill the visible space in the stack panel. At that point, instead of a scrollbar appearing, I get the following exception:
System.Windows.Markup.XamlParseException: ''{DependencyProperty.UnsetValue}' is not a valid value for
property 'Foreground'.'
This exception was originally thrown at this call stack:
[External Code]
I suspect this is an issue with some of the styles I've defined to format my text boxes and text blocks within the IngredientUserControl. I keep these styles in a Fonts.xaml resource dictionary which is included in my App.xaml file. My font styles all look roughly like this:
<!--Body1 text (used for static large body text)-->
<Style x:Key = "Body1Text" TargetType = "TextBlock">
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="TextAlignment" Value="Left"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryBrush}"/>
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Margin" Value="5"/>
</Style>
PrimaryBrush is a SolidColorBrush which I define differently in the resources section of all my user controls (I do this because the color theming varies across different components of my app, but the font styles do not). For example, inside my IngredientUserControl I have these resources:
<UserControl.Resources>
<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource OrangeTheme}"></SolidColorBrush>
<SolidColorBrush x:Key="AccentBrush" Color="{StaticResource LightTealTheme}"></SolidColorBrush>
</UserControl.Resources>
And this text block:
<TextBlock Name="Quantity" Margin="0,20,0,5"
Text="{Binding Path=Quantity, Mode=OneWay}"
Style="{StaticResource Body1Text}">
</TextBlock>
All of the styles work and display correctly until the Ingredients Panel runs out of space! When the ScrollViewer gets full, it breaks and can no longer find some foreground property somewhere. I don't understand why I'm only having this issue when the ScrollViewer needs to scroll. Any ideas?