0

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?

  • How do you add `TextBlock`s to the `StackPanel`? – Rekshino Jun 22 '20 at 09:41
  • Please take a minute and read about [**M**inimal **C**omplete **V**erifiable **E**xample](https://stackoverflow.com/help/mcve) – Rekshino Jun 22 '20 at 09:47
  • @Rekshino this question provides enough information already to answer without citing the rules. The XAML dependency path is entirely there, and the poster has explained the specific conditions on which the error occurs. I feel my answer explains a good line of enquiry to find the solution, so I am confused someone has down-voted it without adding or suggesting improvement. – Kodaloid Jun 22 '20 at 12:36
  • @Kodaloid, your answer seems to have disappeared, but it was very helpful, thank you! – Alicia Nimrick Jun 22 '20 at 19:31
  • Looks like a moderator deleted my answer, anyway I am glad you found the solution! – Kodaloid Jun 23 '20 at 22:21

1 Answers1

0

Turns out it was an issue with the style on the ScrollViewer. I had not even attempted to style the ScrollViewer, but whatever default style it was trying to apply was missing a resource. Very weird. I built my own style for the ScrollViewer and now it's working correctly.