wpf XAML - 多次设置属性“内容”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14868713/
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
XAML - The property 'Content' is set more than once
提问by berniefitz
Very new to WPF and XAML. I can't get my head around why I can't place a WPF control where I would like in the following code. My issue is where the <canvas></canvas>
tags are. Anything I put in this place gives me 'The property 'Content' is set more than once'
WPF 和 XAML 非常新。我不明白为什么我不能在下面的代码中放置 WPF 控件。我的问题是<canvas></canvas>
标签在哪里。我放在这个地方的任何东西都给了我'属性'内容'设置不止一次'
If anyone could explain in simple terms where the Content property is set that would be most helpful.
如果有人可以用简单的术语解释 Content 属性的设置位置,那将是最有帮助的。
I have checked out the following articles to no avail: the property 'Content' is set more than oncethe property content is set more than onceProperty content is set more than onceThe property 'Content' is set more than once Button WPFControlTemplate causeing error "The property 'content' is set more than once"
我查看了以下文章无济于事: 多次设置属性“内容” 多次设置属性内容多次设置属性内容 多次设置“内容” 属性 Button WPF ControlTemplate 导致错误“多次设置属性‘内容’”
<Window x:Class="PDFIndexer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="ParentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Menu Grid.Row="0" >
<MenuItem Header="File" >
<MenuItem Header="Open Project" Click="MenuItem_Click_1"></MenuItem>
<MenuItem Header="Save Project"></MenuItem>
<MenuItem Header="Close Project"></MenuItem>
<Separator></Separator>
<MenuItem Header="Exit"></MenuItem>
</MenuItem>
<MenuItem Header="Edit"></MenuItem>
</Menu>
<TabControl Grid.Row="1">
<TabItem Header="Document Flow" >
This is where the outline of the entire document will be placed.
<Canvas></Canvas>
</TabItem>
<TabItem Header="Preview">
This is where the preview will be drawn to screen.
</TabItem>
<TabItem Header="Resources">
This is where the resources { graphic files, fonts, data files }
</TabItem>
<TabItem Header="Code Library">
This is where the user can save re-usable bits of code. Useful when adding intel barcodes or Address blocks etc...
</TabItem>
</TabControl>
<StatusBar Grid.Row="2">
Items
</StatusBar>
</Grid>
回答by Mark Hall
By adding your text description to your TabItem
you added Content then when you added the Canvas you added an additional item of Content which is not allowed for the TabItem
. You need to use a Control that can hold a collection of Childrensuch as Canvas, Grid, StackPanel etc. Try something like this.
通过将您的文本描述添加到您TabItem
添加的内容,然后当您添加画布时,您添加了一个附加的内容项目,该项目不允许用于TabItem
. 您需要使用一个可以容纳子元素集合的控件,例如 Canvas、Grid、StackPanel 等。尝试这样的操作。
<TabControl Grid.Row="1">
<TabItem Header="Document Flow">
<Canvas>
<TextBlock>
This is where the outline of the entire document will be placed.
</TextBlock>
</Canvas>
</TabItem>
<TabItem Header="Preview">
This is where the preview will be drawn to screen.
</TabItem>
<TabItem Header="Resources">
This is where the resources { graphic files, fonts, data files }
</TabItem>
<TabItem Header="Code Library">
This is where the user can save re-usable bits of code. Useful when adding intel barcodes or Address blocks etc...
</TabItem>
</TabControl>
回答by failedprogramming
Certain containers only allow 1 element, other containers allow >1 element. When you get the error message 'Content' is set more than once, it means you have tried to put more than one type of element in a container that only allows 1 element.
某些容器只允许 1 个元素,其他容器允许 >1 个元素。当您收到错误消息“内容”设置不止一次时,这意味着您尝试将不止一种类型的元素放入仅允许 1 个元素的容器中。
Maybe try this (not tested):
也许试试这个(未测试):
<TabItem Header="Document Flow" >
<StackPanel>
<TextBlock>This is where the outline of the entire document will be placed. </TextBlock>
<Canvas></Canvas>
</StackPanel>
</TabItem>
回答by Andrey Gordeev
Try to wrap a content of TabItem
in a Grid
and use TextBlock
to show text:
尝试包的内容TabItem
中Grid
,并使用TextBlock
显示的文字:
<TabItem Header="Document Flow" >
<Grid>
<TextBlock Text="This is where the outline of the entire document will be placed."/>
<Canvas></Canvas>
</Grid>
</TabItem>