XAML 태그에 정의한 Panel
의 Children
속성에 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>
출처
2017-09-14 14:55:05
mm8
이 ... 주셔서 감사합니다 :) 를 어떻게 MVVM의 관점에서 그것을 말해 줄 수 있습니까? – jclstefan
당신은 뷰가 아닌 곳에서는 컨트롤을 만들지 않습니다 ... 어쩌면 ItemsControl의 작동 방식을 찾아야합니다. – mm8