2014-04-14 4 views
0

예제 사용자 지정 컨트롤이 만들어졌습니다. 그것은 프로젝트를 빌드 한 후 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(); 

     } 
    } 
} 
+4

일반적으로 컨트롤을 XAML에 추가 한 다음 원하는 경우 컨트롤의 'Visibility'를 Collapsed에서 Visible으로 토글하는 것이 좋습니다 표시 할 수 있습니다. 'BoolToVisibilityConverter'와 같은 바인딩을 사용하여 Boolean 속성에서 가시성을 제어 할 수도 있습니다. – Ryan

답변

1

이는 쇼의 의미에 따라 크게 달라질 수 있습니다. 디스플레이에 추가하기 만하면 :

AddChild(cc); 

이렇게하면 창 children 그룹에 추가됩니다. 창이 하나 밖에 없으므로 이것은 아마도을 날려 버릴 것입니다. 당신이 "ContentGrid"라는 루트 그리드가있는 경우, 다음은 다음과 같습니다

ContentGrid.Children.Add(cc); 

두 가지 접근 방식의 문제는 당신이 위치를 제어하지 않는다는 것입니다. 물론 마진 속성 등을 설정하여 수정할 수 있습니다. 사용자 지정 컨트롤이 창 (대신 컨트롤)에서 상속한다면 당신은 대화 상자하려는 경우, 당신은 쇼 대화를 할 수있는 : 물론

cc.ShowDialog(); 

을,이 모든 접근은 단지 그것을 보여 것보다 더 나은 코드 숨김에서 UI를 수정하는 대신 XAML을 사용합니다.

+0

Window는 ContentControl이므로 1 개의 자식 만 가질 수 있습니다. AddChild에 대한 호출을 추가하거나 지원하지 않는 Children 콜렉션이 없습니다 (드문 경우입니다). –

+1

따라서 두 번째 단락에서 내 코멘트 :). 나는 당신이 실제로 첫 번째 라인을 할 수 없다는 것을 분명히하기 위해 그것을 굵게 썼다. – BradleyDotNET

관련 문제