wpf 内容设置不止一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25919422/
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
Content is set more than once
提问by Daryl Gill
Whilst playing about with WPF in Visual Studio 2013, I have been presented with an error:
在 Visual Studio 2013 中使用 WPF 时,我遇到了一个错误:
Error 2 The property 'Content' is set more than once.
Error 1 The property "Content" can only be set once
错误 2 多次设置属性“内容”。
错误 1 属性“内容”只能设置一次
Now, first of all. I turn to google for the error message & got presented with a top results linking to StackOverflow.
现在,首先。我求助于 google 以获取错误消息,并获得了链接到 StackOverflow 的最佳结果。
XAML - The property 'Content' is set more than once
the property 'Content' is set more than once
Property content is set more than once
Including a MSDN post:
包括 MSDN 帖子:
Whilst being presented with an informativecollection of tailored solutions based on the original posters code, I have yet to come across an actual basic solution detailing the reasons of this error (XAML Novice) Whilst this may be a duplicate of multiple reported problems. I personally would rather avoid posting a problematic code to get a tailored solution. I'd much rather come here and ask the community reasons as to why a novice XAMP/WPF Developer might encounter this application and solutions & not so much top, top best practices. Rather words of advice from the more WPF/XAMP Developers on how to easily identify the solution and continue on with furter debugging steps in the future
虽然看到了基于原始海报代码的定制解决方案的信息丰富的集合,但我还没有遇到一个实际的基本解决方案,详细说明此错误的原因(XAML 新手)虽然这可能是多个报告问题的重复。我个人宁愿避免发布有问题的代码以获得量身定制的解决方案。我更愿意来这里询问社区原因,为什么新手 XAMP/WPF 开发人员可能会遇到此应用程序和解决方案,而不是过多的顶级最佳实践。而是来自更多 WPF/XAMP 开发人员关于如何轻松识别解决方案并在未来继续进行更多调试步骤的建议
For argument sake:
为了论证:
<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>
回答by Northik
A window can only contains 1 element.
一个窗口只能包含 1 个元素。
In your code
在你的代码中
<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>
your window have 2 textblock u shoudl try something like
您的窗口有 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>
回答by Jaime Still
If you are setting more than one element inside of any UIElement with the Content dependency property, you will get this error. You need to wrap multiple elements inside of a panel so that the Content property only has one child element. For instance...
如果您在具有内容依赖项属性的任何 UIElement 内设置多个元素,您将收到此错误。您需要在面板内包装多个元素,以便 Content 属性只有一个子元素。例如...
<Button>
<StackPanel Orientation="Horizontal">
<Image />
<TextBlock />
</StackPanel>
</Button>
<Border>
<StackPanel>
<TextBlock />
<Image />
<DatePicker />
</StackPanel>
</Border>
The areas between the Button and Border elements are shorthand for specifying:
Button 和 Border 元素之间的区域是指定的速记:
<Button>
<Button.Content>
<!-- Content goes here -->
</Button.Content>
</Button>

