WPF 模板错误 - “在 'System.Windows.Baml2006.TypeConverterMarkupExtension' 上提供值引发异常。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2402924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:33:35  来源:igfitidea点击:

WPF Templates error - "Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception."

wpftemplatesvisual-studio-2010binding

提问by jasonk

I've just started experimenting with WPF templates vs. styles and I'm not sure what I'm doing wrong. The goal below is to alternate the colors of the options in the menu. The code works fine with just the , but when I copy and paste/rename it for the second segment of "MenuChoiceOdd" I get the following error:

我刚刚开始尝试 WPF 模板与样式,我不确定我做错了什么。下面的目标是交替菜单中选项的颜色。该代码仅适用于 ,但是当我为“MenuChoiceOdd”的第二段复制并粘贴/重命名它时,出现以下错误:

> Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.

> 在“System.Windows.Baml2006.TypeConverterMarkupExtension”上提供值引发异常。

Sample of the code:

代码示例:

<Window x:Class="WpfApplication1.Template_Testing"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Template_Testing" Height="300" Width="300">
    <Grid>
        <Grid.Resources>
            <ControlTemplate x:Key="MenuChoiceEven">
                <Border BorderThickness="1" BorderBrush="#FF4A5D80">
                <TextBlock Height="Auto" HorizontalAlignment="Stretch" Margin="0"  Width="Auto" FontSize="14" Foreground="SlateGray" TextAlignment="Left" AllowDrop="True" Text="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}">
                    <TextBlock.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="White" Offset="0" />
                            <GradientStop Color="#FFC2CCDB" Offset="1" />
                        </LinearGradientBrush>
                    </TextBlock.Background>
                </TextBlock>
                </Border>
            </ControlTemplate>
            <ControlTemplate x:Key="MenuChoiceOdd">
                <Border BorderThickness="1" BorderBrush="#FF4A5D80">
                    <TextBlock Height="Auto" HorizontalAlignment="Stretch" Margin="0"  Width="Auto" FontSize="14" Foreground="SlateGray" TextAlignment="Left" AllowDrop="True" Text="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}">
                    <TextBlock.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="White" Offset="0" />
                            <GradientStop Color="##FFCBCBCB" Offset="1" />
                        </LinearGradientBrush>
                    </TextBlock.Background>
                    </TextBlock>
                </Border>
            </ControlTemplate>            
        </Grid.Resources>
        <Border BorderBrush="SlateGray" BorderThickness="2" Margin="10" CornerRadius="10" Background="LightSteelBlue" Width="200">
            <StackPanel Margin="4">
                <TextBlock Height="Auto" HorizontalAlignment="Stretch" Margin="2,2,2,0" Name="MenuHeaderTextBlock" Text="TextBlock" Width="Auto" FontSize="16" Foreground="PaleGoldenrod" TextAlignment="Left" Padding="10" FontWeight="Bold"><TextBlock.Background><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="LightSlateGray" Offset="0" /><GradientStop Color="DarkSlateGray" Offset="1" /></LinearGradientBrush></TextBlock.Background></TextBlock>
                <StackPanel Height="Auto" HorizontalAlignment="Stretch" Margin="2,0,2,0" Name="MenuChoicesStackPanel" VerticalAlignment="Top" Width="Auto">
                    <Button Template="{StaticResource MenuChoiceEven}" Content="Test Even menu element" />
                    <Button Template="{StaticResource MenuChoiceOdd}"  Content="Test odd menu element" />
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</Window>

What am I doing wrong?

我究竟做错了什么?

采纳答案by jasonk

Oy it's Monday...

哦,今天是星期一...

When I copied pasted the color string from another control it put two "##"s in the prefix here:

当我从另一个控件复制粘贴颜色字符串时,它在此处的前缀中放置了两个“##”:

<GradientStop Color="##FFCBCBCB" Offset="1" />

Still a lousy error message.

仍然是一个糟糕的错误信息。

回答by DefenestrationDay

Similarly to others - this horrific error message just meant I had a syntax error in my xaml:

与其他人类似 - 这个可怕的错误消息只是意味着我的 xaml 中有语法错误:

<CheckBox Height="" IsChecked="{Binding IsChecked, Mode=TwoWay}"  Width="80">

Notice I hadn't filled in the height value..

注意我没有填写高度值..

回答by Collin O'Connor

I had the same error caused by:

我有同样的错误导致:

<Border Margin="5" BorderBrush="Black" BorderThickness="" Width="Auto"/>

fixed by putting a number in BorderThickness

通过在 BorderThickness 中放置一个数字来修复

回答by Nasenbaer

In my case, I changed the project name. After I got this error. I re-selected the images and saw that I needed to change the resource name of images too.

就我而言,我更改了项目名称。在我得到这个错误之后。我重新选择了图像,发现我也需要更改图像的资源名称。

        <Image x:Name="imgSrc" Grid.Row="1" Source="/PREV%20-%20Filter;component/Images/Subtraktive%20Farbmischung%20%28Malen%20mit%20Wasserfarben%29.jpg" />

/PREV%20-%20Filter;component needs to be corrected to /DSP%20-%20Filter;component in my case.

/PREV%20-%20Filter;component 需要更正为 /DSP%20-%20Filter;component 在我的情况下。

回答by skjagini

Assigning Width="*" on Grid gave me the error, which looks weird at the first time, but all it is trying to say is that the markup couldn't be converted to actual type.

在 Grid 上分配 Width="*" 给了我错误,第一次看起来很奇怪,但它想说的是标记无法转换为实际类型。

回答by Bhramar

Unlike others, I had my XAML syntax correct.

与其他人不同,我的 XAML 语法是正确的。

<ImageBrush ImageSource="..\Images\previous_icon.png" />          

This error started appearing when i changed my image - later realised i did change the image in my folder but forgot to Includeit in my project .

当我更改图像时开始出现此错误 - 后来意识到我确实更改了文件夹中的图像,但忘记将其包含在我的项目中。

Hence although XAML could show me the image in 'Desgn' View - I faced this exception @ run time. Silly one.

因此,尽管 XAML 可以在“Desgn”视图中向我显示图像 - 我在运行时遇到了这个异常。 傻一个。

回答by Byrdman

Here is another instance of this error and how I resolved it.

这是此错误的另一个实例以及我如何解决它。

This appears to have been caused by upgrading my project from VS2010 to VS2012 and VS2013. Not sure exactly why that would be an issue here, but I guess it is.

这似乎是由于将我的项目从 VS2010 升级到 VS2012 和 VS2013 引起的。不确定为什么这会成为一个问题,但我猜是。

I developed a Visual Studio Wizard to automate the process of adding a project to our very large solution (300+ projects) as there are additional requirements for us developers to help maintain the project. I used WPF for the wizard and that worked well for VS2010. I upgraded the project to VS2012 and VS2013 and I started getting this error. Turns out that at some point the path to my icon and image files was screwed up. Once I corrected it - which I did directly within the XAML - I stopped getting the error.

我开发了一个 Visual Studio 向导来自动化将项目添加到我们非常大的解决方案(300 多个项目)的过程,因为我们的开发人员需要额外的要求来帮助维护项目。我使用 WPF 作为向导,并且在 VS2010 中运行良好。我将项目升级到 VS2012 和 VS2013,我开始收到此错误。事实证明,在某些时候,我的图标和图像文件的路径被搞砸了。一旦我纠正了它——我直接在 XAML 中做了——我就不再收到错误了。

In case you may need help referencing resources - this is the post I used to correct my issues: How to reference an icon resource file reference in XAML

如果您在引用资源时可能需要帮助 - 这是我用来纠正我的问题的帖子:How to reference an icon resource file reference in XAML

回答by James

I had the same error, but was using the wrong path for an image in the resources folder

我有同样的错误,但在资源文件夹中使用了错误的图像路径

I had this...

我有这个...

<controls:ImageButton Grid.Column="2" ImageButtonSource="Resources/close_normal.png"/>

but i needed this...

但我需要这个...

<controls:ImageButton Grid.Column="2" ImageButtonSource="../Resources/close_normal.png"/>

Had to add ../ at the start to go back one folder first

必须在开始时添加 ../ 才能先返回一个文件夹

回答by Tejo

I had the same error when using Grey instead of Gray in my BorderBrush

在我的 BorderBrush 中使用 Gray 而不是 Gray 时我遇到了同样的错误

<Setter Property="BorderBrush" Value="Grey"/>

instead of

代替

<Setter Property="BorderBrush" Value="Gray"/>

回答by Vasyl Boroviak

I had the same when I forgot curly brackets:

当我忘记大括号时,我也有同样的感受:

<DataTrigger Binding="{Binding Path=IsWhiteboardShared}" Value="True">
    <Setter Property="Background" Value="StaticResource AccentColorBlueBrush"/>
</DataTrigger>