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

21,218

Solution 1

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?

Solution 2

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.

Solution 3

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>

Solution 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>
Share:
21,218

Related videos on Youtube

DaveO
Author by

DaveO

Updated on September 24, 2020

Comments

  • DaveO
    DaveO almost 4 years

    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
    DaveO over 13 years
    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
    DaveO over 13 years
    I lie, it was set in two places, removing the 2nd one solved the issue thx!
  • Vimes
    Vimes over 6 years
    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.