21

I have a weird error I'm trying to debug with no luck.

I have subclassed hwndhost showing some content, I have the following function in that class to set to fullscreen:

    private void SetFullScreen(bool enable)
    {
        if (enable)
        {
            fs = new Window();
            fs.ResizeMode = ResizeMode.NoResize;
            fs.WindowState = System.Windows.WindowState.Maximized;
            fs.WindowStyle = System.Windows.WindowStyle.None;
            fs.Topmost = true;
            fs.PreviewKeyDown += delegate(object sender, KeyEventArgs e) { 
                if (e.Key==Key.Escape)
                    FullScreen = false;
            };
            fs.Show();
        }
        else
        {
            fs.Close();
            fs = null;
        }
    }

This worked fine in my prototype WPF app but when I use this code in my main app I get this error when closing the window (escape key) and on fs.close() call:

'{DependencyProperty.UnsetValue}' is not a valid value for property 'FocusVisualStyle'.

The weird thing is it happens about 1500ms AFTER the window closes. I've tried setting FocusVisualStyle on fs to null, but it looks like something else. Gut feeling is it's trying to focus another element in my app that doesn't have this property, but really I have no idea!

Thanks!

Edit. Problem was custom setting of FocusVisualStyle on my fullscreen button. I set to {x:Null} and the problem went away.

DaveO
  • 1,909
  • 4
  • 33
  • 63

6 Answers6

31

This can happen when a Style is pointing to a StaticResource that does NOT exist.

This xaml was failing:

<Grid.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Height" Value="{StaticResource StandardControlHeight}"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
    </Style>
</Grid.Resources>

The error was:

System.InvalidOperationException: ''{DependencyProperty.UnsetValue}' is not a valid value for property 'Height'.'

When I added the missing StaticResource, the problem went away.

Igor Popov
  • 9,795
  • 7
  • 55
  • 68
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
14

my guess is that the control that gets the focus when you close the mentioned window has a custom style set by you that does not include any FocusVisualStyle.

so to help you further, you should explain a bit more: what happens (or should happen) when you close this window?

what control type is supposed to get the focus?

David
  • 6,014
  • 4
  • 39
  • 55
  • A toggle button kicks off the fullscreen command so I guess that gets the focus on return. However, later a keyboard command (e.g. F12) might kick it off so it could be any element that has the current focus. That togglebutton has a custom style, I tried setting the FocusVisualStyle on that style to {x:Null} with no luck. – DaveO Feb 23 '11 at 18:57
  • 1
    I lie, it was set in two places, removing the 2nd one solved the issue thx! – DaveO Feb 23 '11 at 19:02
  • 4
    For readers: I got the error because, in one spot, I wasn't merging the ResourceDictionary that defined my actual focus styles. XAML designer didn't complain about the StaticResource references. I got no error until I changed keyboard focus in a particular way at runtime. – Vimes Jan 17 '18 at 00:06
10

Yet another way to cause mentioned exception is when you declare StaticResource after using it, for example in style declaration.

WRONG

<Style TargetType="Label">
    <Setter Property="Foreground" Value="{StaticResource BlueAccent}"/>
</Style>

<SolidColorBrush x:Key="BlueAccent" Color="#22afed"/>

CORRECT

<SolidColorBrush x:Key="BlueAccent" Color="#22afed"/>

<Style TargetType="Label">
    <Setter Property="Foreground" Value="{StaticResource BlueAccent}"/>
</Style>
Soleil
  • 6,404
  • 5
  • 41
  • 61
K Black
  • 115
  • 1
  • 7
  • what you describe can get even more complex when you have multiple resource dictionaries, and end up with "strange" consequences and should therefore be avoided at all cost (see this question for instance : https://stackoverflow.com/questions/8299664/style-targettype-causes-xamlparseexception-when-not-attached-to-debugger/8443351#8443351) – David May 16 '23 at 09:08
4

In case you got here by Googling the question title: Another way to cause this exception is to use a Trigger, but forget to set the Value.

Example:

<ControlTemplate.Triggers>
  <Trigger Property="IsEnabled">
    <Setter Property="Background" Value="Gray" />
  </Trigger>
</ControlTemplate.Triggers>

This causes a XamlParseException where the inner exception reads:

'{DependencyProperty.UnsetValue}' is not a valid value for property 'IsEnabled'.

Correction:

<ControlTemplate.Triggers>
  <Trigger Property="IsEnabled" Value="False">
    <Setter Property="Background" Value="Gray" />
  </Trigger>
</ControlTemplate.Triggers>
Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70
1

Another situation is when the StaticResource is declared but not seen when mentioned..

For example, in my case:

'{DependencyProperty.UnsetValue}' is not a valid value for property 'Background'.

Occurs at OnStartup method in App.xaml.cs file, before the app launch..

The exception message mentioned the Background property, it tells that:

At some

<Setter Property="Background" Value="{StaticResource xxx}" />

or

Background="{StaticResource xxx}"

WPF started looking for xxx in the hierarchy, and if somthing like

<SolidColorBrush x:Key="xxx">yyy</SolidColorBrush>

was not found, the exception will occur.

In my case, I have put my styles in a separate project, It was like this:

- SharedModule
    - SharedResources.xaml <---- this will be in App.xaml/MergedDictionaries 
        - MergedDictionaries
            - ButtonStyles.xaml <---- xxx was defined and used here
            - ToggleButtonStyles.xaml <--- xxx was used here as well

I thought that the styles in ToggleButtonStyles.xaml will see xxx because of the order of the declaration (ButtonStyles.xaml is above and will be merged before ToggleButtonStyles.xaml), it turns out to be wrong!

Solution 1.

Replace StaticResource with DynamicResource

<Setter Property="Background" Value="{DynamicResource xxx}" />

This way, WPF will not throw an exception if xxx is not found at launch time, but it will use it afterwards as expected.

Solution 2.

Extract all the colors to a separate ResrouceDictionary and merge it in App.xaml before SharedResources.xaml

- Application
    - App.xaml
        - ResourceDictionary
            - MergedDictionaries
                - Colors.xaml <--- xxx is defined one level above SharedResources/MergedDictionaries
                - SharedResources.xaml
Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
1

Yet another reason of this exception can be setting a property value to a wrong type. For instance, consider the following XAML code:

<Grid>
    <Grid.Resources>
        <sys:Double x:Key="DoubleWidth">200</sys:Double>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{StaticResource DoubleWidth}"/> -> Exception here
    </Grid.ColumnDefinitions>
    <TextBlock Text="Abc"/>
</Grid>

In this code the line starting with ColumnDefinition will cause the aforementined exception (ArgumentException: '200' is not a valid value for property 'Width') to be thrown since the type of the ColumnDefinition.Width property is not a System.Double value but a System.Windows.GridLength struct. To fix the issue, first define a new resource in the Grid.Resources block as

<GridLength x:Key="GridLengthWidth">200</GridLength>

and replace the erroneous line with this line:

<ColumnDefinition Width="{StaticResource GridLengthWidth}"/>

Actually when we set a property to a wrong type, Visual Studio warns us with underlining the property and saying "The resource 'DoubleWidth' has an incompatible type." but compiles anyway.

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16