'{DependencyProperty.UnsetValue}'不是属性“FocusVisualStyle”的有效值;

发布于 2024-10-19 07:39:59 字数 1116 浏览 2 评论 0原文

我有一个奇怪的错误,我正在尝试调试,但没有成功。

我已经对显示一些内容的 hwndhost 进行了子类化,我在该类中具有以下函数来设置为全屏:

    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;
        }
    }

这在我的原型 WPF 应用程序中运行良好,但是当我在主应用程序中使用此代码时,我在关闭窗口时收到此错误(转义键)并且在 fs.close() 调用上:

'{DependencyProperty.UnsetValue}' 不是属性 'FocusVisualStyle' 的有效值。

奇怪的是它发生在大约 1500 毫秒窗口关闭后。我尝试将 fs 上的 FocusVisualStyle 设置为 null,但它看起来像其他东西。直觉是它试图集中我的应用程序中没有此属性的另一个元素,但实际上我不知道!

谢谢!

编辑。问题是我的全屏按钮上 FocusVisualStyle 的自定义设置。我设置为 {x:Null} 然后问题就消失了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一枫情书 2024-10-26 07:39:59

当样式指向不存在StaticResource时,就会发生这种情况。

此 xaml 失败:

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

错误是:

System.InvalidOperationException: ''{DependencyProperty.UnsetValue}'
不是属性“高度”的有效值。

当我添加缺少的 StaticResource 时,问题就消失了。

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.

一场春暖 2024-10-26 07:39:59

我的猜测是,当您关闭提到的窗口时获得焦点的控件具有您设置的自定义样式,该样式不包含任何 FocusVisualStyle。

因此,为了进一步帮助您,您应该多解释一下:关闭此窗口时会发生(或应该发生)什么?

哪种控件类型应该获得焦点?

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?

云仙小弟 2024-10-26 07:39:59

导致上述异常的另一种方法是在使用 StaticResource 之后声明它,例如在样式声明中。

错误

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

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

正确

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

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

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>
淡莣 2024-10-26 07:39:59

如果您通过谷歌搜索问题标题来到这里:导致此异常的另一种方法是使用触发器,但忘记设置

示例:

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

这会导致 XamlParseException,其中内部异常如下:

“{DependencyProperty.UnsetValue}”不是属性的有效值
“已启用”。

更正:

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

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>
回忆凄美了谁 2024-10-26 07:39:59

另一种情况是声明了 StaticResource 但在提及时未看到

例如,在我的例子中:

“{DependencyProperty.UnsetValue}”不是属性“Background”的有效值。

在应用程序启动之前,在 App.xaml.cs 文件中的 OnStartup 方法中发生。

异常消息提到了 Background 属性,它告诉我们:

在某些

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

Background="{StaticResource xxx}"

WPF开始在层次结构中寻找xxx,如果

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

没有找到类似的东西,就会发生异常。

就我而言,我已将样式放在一个单独的项目中,就像这样:

- 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

我认为 ToggleButtonStyles.xaml 中的样式会因为声明的顺序而看到 xxx (ButtonStyles .xaml在上面,会在ToggleButtonStyles.xaml之前合并),结果是错误的!

解决方案1.

StaticResource替换为DynamicResource

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

这样,如果在启动时找不到xxx,WPF不会抛出异常,而是会使用之后就如预期的那样。

解决方案 2.

将所有颜色提取到单独的 ResrouceDictionary 中,并将其合并到 SharedResources.xaml 之前的 App.xaml 中

- Application
    - App.xaml
        - ResourceDictionary
            - MergedDictionaries
                - Colors.xaml <--- xxx is defined one level above SharedResources/MergedDictionaries
                - SharedResources.xaml

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
离不开的别离 2024-10-26 07:39:59

此异常的另一个原因可能是将属性值设置为错误的类型。例如,请考虑以下 XAML 代码:

<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>

在此代码中,以 ColumnDefinition 开头的行将导致上述异常(由于 ColumnDefinition.Width 属性的类型不是 System.Double 值而是 System.Windows.GridLength 结构,因此抛出 ArgumentException: '200' 不是属性 'Width' 的有效值) . 要解决此问题,首先在 Grid.Resources 块中定义一个新资源

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

,并将错误行替换为以下行:

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

实际上,当我们将属性设置为错误类型时,Visual Studio 通过在属性下划线来警告我们并指出“资源‘DoubleWidth’具有不兼容的类型。”但无论如何都能编译。

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.

唔猫 2024-10-26 07:39:59

对于我来说,这段代码中发生了同样的错误:

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

我以这种方式编写了样式:

<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>

现在我收到如下错误:
“{DependencyProperty.UnsetValue}”不是属性“Visibility”的有效值,

因此我了解 Visibility 属性设置不正确。然后我做了一些排列和组合,找到了正确的方法,如下所示:

<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>

在 usercontrol xaml 中,我更改了代码,如下所示:

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

所以我理解的基本点是它显示错误的属性退出查找

  1. 静态资源指向key 正确
  2. 查看静态资源是否存在
  3. 确保属性和值设置正确。
  4. 确保属性的默认值设置正确。
  5. 有些属性在 style 部分不起作用,而您必须直接将它们放在用户控件中。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文