나는 당신이 당신의 자신의 쓰기 좋을 것. 레이블이 붙은 컨트롤의 수직리스트가있는 폼의 가장 기본적인 경우, 직관적 인 접근법은 문자열 객체 쌍의 정렬 된 목록으로 구성되는 데이터 객체를 갖는 것입니다 (어쩌면 유효성 검사 규칙을 사용하여 트리플로 만들 수도 있습니다). 양식을로드해야 할 때 각 개체의 형식이 검사되고 문자열 인 경우 텍스트 상자를 만들고 bool 인 경우 확인란을 만듭니다. 예를 들어 int 및 double을 사용하면 입력 유효성 검사를 사용할 수도 있습니다. 다른 방향도 너무 어렵지 않아야합니다.
나는 다음 (WPF)처럼 전 반 동적 일반적인 편집 대화를 작성했습니다 : 창 내용 XAML : 코드 숨김
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Name="StackPanelInput" Grid.Row="0" Orientation="Vertical" VerticalAlignment="Top" Margin="5"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="5">
<Button Name="ButtonOK" HorizontalAlignment="Right" Width="100" Margin="5" Click="ButtonOK_Click" IsDefault="True">OK</Button>
<Button Name="ButtonCancel" HorizontalAlignment="Right" Width="100" Margin="5" Click="ButtonCancel_Click" IsCancel="True">Cancel</Button>
</StackPanel>
</Grid>
:
public partial class EditDialog : Window
{
private List<Control> Controls = new List<Control>();
public EditDialog()
{
InitializeComponent();
Loaded += delegate { KeyboardFocusFirstControl(); };
}
public EditDialog(string dialogTitle) : this()
{
Title = dialogTitle;
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)
{
(sender as Button).Focus();
if (!IsValid(this))
{
MessageBox.Show("Some inputs are currently invalid.");
return;
}
DialogResult = true;
}
private void ButtonCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
bool IsValid(DependencyObject node)
{
if (node != null)
{
bool isValid = !Validation.GetHasError(node);
if (!isValid)
{
if (node is IInputElement) Keyboard.Focus((IInputElement)node);
return false;
}
}
foreach (object subnode in LogicalTreeHelper.GetChildren(node))
{
if (subnode is DependencyObject)
{
if (IsValid((DependencyObject)subnode) == false) return false;
}
}
return true;
}
public TextBox AddTextBox(string label, ValidationRule validationRule)
{
Grid grid = new Grid();
grid.Height = 25;
grid.Margin = new Thickness(5);
ColumnDefinition colDef1 = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) };
ColumnDefinition colDef2 = new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) };
grid.ColumnDefinitions.Add(colDef1);
grid.ColumnDefinitions.Add(colDef2);
Label tbLabel = new Label() { Content = label };
tbLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
grid.Children.Add(tbLabel);
TextBox textBox = new TextBox();
Binding textBinding = new Binding("Text");
textBinding.Source = textBox;
textBinding.ValidationRules.Add(validationRule);
textBox.SetBinding(TextBox.TextProperty, textBinding);
textBox.GotKeyboardFocus += delegate { textBox.SelectAll(); };
textBox.TextChanged += delegate { textBox.GetBindingExpression(TextBox.TextProperty).ValidateWithoutUpdate(); };
textBox.Text = "";
textBox.GetBindingExpression(TextBox.TextProperty).ValidateWithoutUpdate();
Grid.SetColumn(textBox, 1);
grid.Children.Add(textBox);
StackPanelInput.Children.Add(grid);
Controls.Add(textBox);
return textBox;
}
public TextBox AddTextBox(string label, ValidationRule validationRule, string defaultText)
{
TextBox tb = AddTextBox(label, validationRule);
tb.Text = defaultText;
return tb;
}
public CheckBox AddCheckBox(string label)
{
Grid grid = new Grid();
grid.Height = 25;
grid.Margin = new Thickness(5);
CheckBox cb = new CheckBox();
cb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
cb.Content = label;
grid.Children.Add(cb);
StackPanelInput.Children.Add(grid);
Controls.Add(cb);
return cb;
}
private void KeyboardFocusFirstControl()
{
if (Controls.Count > 0)
{
Keyboard.Focus(Controls[0]);
}
}
}
사용 예 :
EditDialog diag = new EditDialog();
TextBox firstName = diag.AddTextBox("First Name:", new StringValidationRule());
TextBox lastName = diag.AddTextBox("Last Name:", new StringValidationRule());
TextBox age = diag.AddTextBox("Age:", new IntegerValidationRule(1,int.MaxValue));
if ((bool)diag.ShowDialog())
{
//parse texts and write them to some data;
}
사용자 정의 생성자, 편집 버튼 등을 사용하면 완전히 동적 인 대화 상자로 전환 할 수 있습니다. 레이아웃이 어느 정도 정교 해지는 지에 따라 다소 효과가있을 수 있습니다. 물론 그렇게하는 라이브러리를 찾는 것이 가장 쉽습니다. 아마도 다른 사람이 알고있을 것입니다.