wpf “ContentControl 的内容必须是单个元素。” 例外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12609724/
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 of a ContentControl must be a single element." exception
提问by Idov
I'm new to WPF so this may be a stupid question but here it is.
I'm trying to create custom button using the XAML thing, so this is what I have:
我是 WPF 的新手,所以这可能是一个愚蠢的问题,但就是这样。
我正在尝试使用 XAML 创建自定义按钮,所以这就是我所拥有的:
<Button x:Class="Controls.ShinyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Height="40" Width="150">
<Button.Resources>
<ResourceDictionary Source=".\Resources.xaml" />
</Button.Resources>
<Border Background="{StaticResource BlueGradient}" CornerRadius="5">
<DockPanel>
<Image x:Name="imgIcon" DockPanel.Dock="Left" Height="32" Margin="4"/>
<TextBlock DockPanel.Dock="Right" Text ="Test" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" />
</DockPanel>
</Border>
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF02B6FF" Offset="0" />
<GradientStop Color="#FF0084F0" Offset="1" />
</LinearGradientBrush>
</Button.Background>
</Button>
The designer understands it and it compiles without errors, but when I try to run the program and create an instance of it, I get the error:
"Content of a ContentControl must be a single element."
设计者理解它并且编译没有错误,但是当我尝试运行程序并创建它的实例时,我收到错误:
“ContentControl 的内容必须是单个元素。”
My resources file is:
我的资源文件是:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush x:Key="BlueGradient">
<GradientStop Offset="0" Color="#FF02B6FF"/>
<!-- (2, 182, 255)-->
<GradientStop Offset="1" Color="#FF0084F0"/>
<!-- (0, 132, 240) -->
</LinearGradientBrush>
</ResourceDictionary>
I have the DockPanel inside my Border so the button has only one child...
The "ShinyButton" class looks like this:
我的边框内有 DockPanel,所以按钮只有一个孩子......
“ShinyButton”类看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Controls
{
/// <summary>
/// Interaction logic for ShinyButton.xaml
/// </summary>
public partial class ShinyButton : Button
{
public ShinyButton()
{
InitializeComponent();
}
}
}
In the main function I add it like this:
在主函数中,我像这样添加它:
public MainWindow()
{
InitializeComponent();
ShinyButton shiny = new ShinyButton();
this.AddChild(shiny); //This is where the exception is thrown.
}
What am I doing wrong?
我究竟做错了什么?
回答by Florian Gl
Just a guess and atm I am not able to test it, but try following code:
只是猜测和 atm 我无法测试它,但请尝试以下代码:
<Button.Template>
<ControlTemplate>
<Border Background="{StaticResource BlueGradient}" CornerRadius="5">
<DockPanel>
<Image x:Name="imgIcon" DockPanel.Dock="Left" Height="32" Margin="4"/>
<TextBlock DockPanel.Dock="Right" Text ="Test" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" />
</DockPanel>
</Border>
</ControlTemplate>
</Button.Template>
EDIT: Ok next try:
编辑:好的下一次尝试:
Instead of adding your ShinyButton to the Window (remove this.AddChild(shiny)), try adding your ShinyButton to the Grid by adding this code:
不要将您的 ShinyButton 添加到窗口(删除 this.AddChild(shiny)),而是尝试通过添加以下代码将您的 ShinyButton 添加到网格:
NameOfGrid.Children.Add(shiny);
The reason of this error is that your Window just can have one child and I bet in your case there is already a Grid as this child.
这个错误的原因是你的 Window 只能有一个孩子,我敢打赌你的情况已经有一个 Grid 作为这个孩子。
回答by AlSki
Just tried your code and it works fine, with a couple of caveats.
刚刚尝试了您的代码,它运行良好,但有一些注意事项。
In order to get it to work I needed to drop the resouceDictionary link, as I don't have that file. Is there any chance that some content is being defined, rather than just a style/template etc?
为了让它工作,我需要删除 resouceDictionary 链接,因为我没有那个文件。是否有可能定义某些内容,而不仅仅是样式/模板等?
Also I note that your code has an x:Class="ShinyButton", is there any content defined in the code as well as in the xaml?
另外我注意到你的代码有一个 x:Class="ShinyButton",在代码和 xaml 中是否定义了任何内容?

