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

7

24

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.

Functionary answered 23/2, 2011 at 9:52 Comment(0)
F
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?

Fantasist answered 23/2, 2011 at 10:11 Comment(3)
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.Functionary
I lie, it was set in two places, removing the 2nd one solved the issue thx!Functionary
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.Wholly
R
32

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.

Recollected answered 12/12, 2018 at 23:53 Comment(2)
As always, WPF have not very clear exception messages.Resolutive
Or, StaticResource exists but not seen by the style that mentioned it.. I've just added my answer to illustrate this case.Imperator
F
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?

Fantasist answered 23/2, 2011 at 10:11 Comment(3)
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.Functionary
I lie, it was set in two places, removing the 2nd one solved the issue thx!Functionary
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.Wholly
F
13

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>
Foremost answered 11/3, 2020 at 14:3 Comment(1)
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 : #8300164)Fantasist
C
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>
Ci answered 9/1, 2020 at 13:51 Comment(0)
I
3

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
Imperator answered 12/12, 2022 at 21:14 Comment(0)
C
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.

Ceporah answered 27/2, 2023 at 6:45 Comment(0)
S
0

For me the same error happened in this code :

<ScrollViewer Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Style="{StaticResource svDicomImage}" x:Name="svDI">

I have written the style in this way :

<Style x:Key="svDicomImage" BasedOn="{StaticResource sv}" 
        TargetType="{x:Type ScrollViewer}">
    <Setter Property="Visibility" 
            Value="{Binding ShowDicomImage, 
            Converter={StaticResource BoolVisibleConverter}}"/>
    <Setter Property="Width" 
            Value="{Binding ActualWidth, 
            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=1}}"/>
    <Setter Property="Height" 
            Value="{Binding ActualHeight, 
            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=1}}"/>
</Style>

Now I was getting error like : '{DependencyProperty.UnsetValue}' is not a valid value for property 'Visibility'

So I understood the Visibility property is not set properly. Then I done some permutation and combination and found the right way as follows :

<Style x:Key="svDicomImage" BasedOn="{StaticResource sv}" 
        TargetType="{x:Type ScrollViewer}">
    <Setter Property="Width" 
            Value="{Binding ActualWidth, 
            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=1}}"/>
    <Setter Property="Height" 
            Value="{Binding ActualHeight, 
            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=1}}"/>
</Style>

and in usercontrol xaml I have changed the code as follows :

<ScrollViewer Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0"
                          Visibility="{Binding ShowDicomImage, Converter={StaticResource BoolVisibleConverter}}"
                          Style="{StaticResource svDicomImage}" x:Name="svDI">

So the basic point I understood is whichever the property it is showing error exits look

  1. The static resource is pointing to the key properly
  2. See the static resource exists
  3. Make sure the property and value are set properly.
  4. Make sure default values of the property is set properly.
  5. Some of the property does not work in style section , rather you have to put them in usercontrol directly.
Sluggard answered 7/7 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.