2017-09-14 5 views
0

이렇게 할 수 있습니까?코드 뒤에서 컨트롤을 만들고 xaml에 전달

public CONTROL selectedControl(string sControl) 
{ 
    CONTROL result = new CONTROL(); 

    if(sContro.Equals("TextBox")) 
    { 
     TextBox txtBx = new TextBox(); 
     // custom TextBox 
     result = txtBx; 
    } 
    else if(sControl.Equals("Button")) 
    { 
     ... 
    } 
    return result; 
} 

어떻게 XAML에 넣을 수 있습니까?

답변

0

XAML 태그에 정의한 PanelChildren 속성에 UIElement을 추가 할 수 있습니다.

다음 샘플 코드를 참조하십시오.

코드 :

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     var child = selectedControl("TextBox"); 
     stackPanel.Children.Add(child); 
    } 

    public UIElement selectedControl(string sControl) 
    { 
     UIElement result = null; 

     if (sControl.Equals("TextBox")) 
     { 
      TextBox txtBx = new TextBox(); 
      // custom TextBox 
      result = txtBx; 
     } 
     //... 
     return result; 
    } 
} 

XAML :

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="300" Width="300"> 
    <StackPanel x:Name="stackPanel"> 

    </StackPanel> 
</Window> 
+0

이 ... 주셔서 감사합니다 :) 를 어떻게 MVVM의 관점에서 그것을 말해 줄 수 있습니까? – jclstefan

+0

당신은 뷰가 아닌 곳에서는 컨트롤을 만들지 않습니다 ... 어쩌면 ItemsControl의 작동 방식을 찾아야합니다. – mm8