“ContentControl的内容必须是单个元素。”异常

3

我是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>

设计师已经理解了它并且编译没有错误,但是当我尝试运行程序并创建它的实例时,我会遇到以下错误:
“ContentControl的内容必须是单个元素。”

我的资源文件是:

<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>

我将DockPanel放在Border中,所以按钮只有一个子元素...
"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();
        }
    }
}

在主函数中,我是这样添加的:
public MainWindow()
        {
            InitializeComponent();

            ShinyButton shiny = new ShinyButton();
            this.AddChild(shiny); //This is where the exception is thrown.

        }

我做错了什么?

我无法使用您展示的代码重现问题。您的“Resources.xaml”文件中有什么? - Luke Woodward
糟糕... 当我在主窗口调用“AddChild”实例时,似乎抛出了异常。 :) - Idov
展示你的ShinyButton。你实现了按钮吗? - paparazzo
你要将ShinyButton添加到哪个元素中呢?也许是ContentControl?^^ - Florian Gl
我添加了“ShinyButton”。我将它添加到一个窗口中。 - Idov
显示剩余3条评论
2个回答

7

我猜想您需要一些帮助,虽然目前无法测试,但请尝试以下代码:

<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>

编辑:下一步尝试:

不要将 ShinyButton 添加到窗口中(删除 this.AddChild(shiny)),而是通过添加以下代码将其添加到网格中:

NameOfGrid.Children.Add(shiny);

这个错误的原因是您的窗口只能有一个子元素,我猜在您的情况下已经有一个Grid作为这个子元素了。

1

刚试了一下你的代码,它运行得很好,但有几个注意点。

为了让它工作,我需要删除resouceDictionary链接,因为我没有那个文件。有没有可能定义了一些内容,而不仅仅是样式/模板等?

此外,我注意到你的代码中有一个x:Class="ShinyButton",除了xaml之外,代码中是否还定义了其他内容?


我添加了资源文件。我有一个名为“ShinyButton”的类,但到目前为止它是空的(它派生自“Button”)... - Idov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接