【问题标题】:add custom control to WPF C# application将自定义控件添加到 WPF C# 应用程序
【发布时间】:2014-04-14 21:25:49
【问题描述】:

我创建了一个示例自定义控件。在我构建项目后它会生成一个 dll。以下是代码。

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

    public class CustomControl1 : Control
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }
}

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:textbtn">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBlock Text="This is a Test" Foreground="Aqua" Background="AntiqueWhite"/>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我将此 dll 包含在另一个 WPF 应用程序中,并希望在用户单击应用程序中的按钮时显示此自定义控件。我怎么做 ?

以下是我的 WPF 应用程序中的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TestCustomControls
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {                      
            textbtn.CustomControl1 cc = new textbtn.CustomControl1();

        }
    }
}

【问题讨论】:

  • 通常最好将控件添加到 XAML,然后当您希望它显示时,只需将控件的 Visibility 从 Collapsed 切换到 Visible。您还可以使用 BoolToVisibilityConverter 等绑定来控制布尔属性的可见性。

标签: c# .net wpf custom-controls


【解决方案1】:

这在很大程度上取决于你所说的表演。只需将其添加到显示中:

AddChild(cc);

这会将它添加到 windows 子组。 这可能会爆炸,因为一个窗口只能有一个孩子。如果您有一个名为“ContentGrid”的根网格,那么它将是:

ContentGrid.Children.Add(cc);

这两种方法的问题在于您无法控制位置。当然,您可以设置边距属性等来解决这个问题。如果您的自定义控件继承自 Window(而不是控件),如果您想要一个对话框,则可以执行显示对话框:

cc.ShowDialog();

当然,比所有这些方法更好的是只在 XAML 中显示它,而不是在代码隐藏中修改 UI :)

【讨论】:

  • Window 是一个 ContentControl,因此只能有 1 个子项。它没有要添加的 Children 集合,并且不支持对 AddChild 的调用,除非没有指定 Content(这种情况很少见)。
  • 因此我在第二段中的评论:)。我将其加粗以确保您实际上无法执行第一行。
猜你喜欢
  • 1970-01-01
  • 2010-11-12
  • 2011-01-23
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多