예제 사용자 지정 컨트롤이 만들어졌습니다. 그것은 프로젝트를 빌드 한 후 DLL을 생성합니다. 다음은 코드입니다. 나는 다른 WPF 응용 프로그램에서이 DLL을 포함하고 사용자가 응용 프로그램에서 버튼을 클릭하면이 사용자 지정 컨트롤을 보여주고 싶어하고WPF C# 응용 프로그램에 사용자 지정 컨트롤 추가
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>
. 어떻게해야합니까?
다음은 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'와 같은 바인딩을 사용하여 Boolean 속성에서 가시성을 제어 할 수도 있습니다. – Ryan