内容设置了多次

本文关键字:设置 | 更新日期: 2023-09-27 18:22:19

在Visual Studio 2013中玩WPF时,我遇到了一个错误:

错误2属性"Content"设置了多次。

错误1属性"内容"只能设置一次

现在,首先。我转向谷歌搜索错误消息&获得了链接到StackOverflow的顶级结果。

XAML-属性';内容';设置多次

财产';内容';设置多次

属性内容设置多次

包括MSDN帖子:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/686be076-954f-4373-a2e6-fc42a6a27718/error-the-property-content-is-set-more-than-once?forum=wpf

虽然我收到了基于原始海报代码的定制解决方案的信息性集合,但我尚未找到详细说明此错误原因的实际基本解决方案(XAML新手)。虽然这可能是多个报告问题的重复。我个人宁愿避免发布有问题的代码,以获得量身定制的解决方案。我更愿意来到这里,向社区询问为什么新手XAMP/WPF开发人员可能会遇到这个应用程序和解决方案&与其说是顶级、顶级的最佳实践。更多WPF/XAMP开发人员建议如何轻松识别解决方案,并在未来的中继续进行更深入的调试步骤


为了论证:

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <TextBlock>Hello</TextBlock>
    <TextBlock>World</TextBlock>
</Window>

内容设置了多次

一个窗口只能包含一个元素。

在您的代码中

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <TextBlock>Hello</TextBlock>
    <TextBlock>World</TextBlock>
</Window>

您的窗口有2个文本块你应该试试之类的东西

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <Grid>
        <TextBlock>Hello</TextBlock>
        <TextBlock>World</TextBlock>
    </Grid>
</Window>

如果在任何具有Content dependency属性的UIElement中设置多个元素,则会出现此错误。您需要将多个元素包装在一个面板内,以便Content属性只有一个子元素。例如。。。

<Button>
    <StackPanel Orientation="Horizontal">
        <Image />
        <TextBlock />
    </StackPanel>
</Button>
<Border>
    <StackPanel>
        <TextBlock />
        <Image />
        <DatePicker />
    </StackPanel>
</Border>

Button和Border元素之间的区域是指定的简写:

<Button>
    <Button.Content>
        <!-- Content goes here -->
    </Button.Content>
</Button>