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

26.5k views Asked by At

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.

6

There are 6 answers

3
David On BEST ANSWER

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?

0
Muhammad Sulaiman On

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
2
Christian Findlay On

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.

0
Mustafa Özçetin On

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.

1
K Black On

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>
0
Sebastian Negraszus On

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>