2011-01-10 5 views
0

우리는 작업 날짜, 참조, 드라이버 할당 등과 같은 특정 섹터 (예 : 전송)에 대한 작업을 기본적으로 모델링 한 데스크톱 응용 프로그램을 가지고 있으며 상태 업데이트를 위해 PDA로 /로부터 전송됩니다.고객, 옵션별로 추가 동적 필드가 지정된 양식을 만드시겠습니까?

주로 선반에서 벗어나지 만, 회사에 맞게 맞춤 부품을 사용해야합니다. 실제로는 로직을 필요로하지 않는 추가 데이터 필드 만 약 90 %가 저장/검색 할 수 있습니다 .

위치, 구성 요소 유형, 기본값을 저장하고 비 편집 모드에서 런타임에 채울 수있는 편집 가능한 모드로 양식을 전환하는 기본 끌어서 놓기 라이브러리를 둘러 보았습니다. 하나를 찾을 수있었습니다.

내 자신을 굴릴 수있는 가장 좋은 방법은 없습니까? 아니면 거기에 60 %의 방법을 제공 할 라이브러리가없는 것입니까? WPF 또는 Winforms 도움을 주시면 감사하겠습니다 (우리는 Winforms를 사용하고 있지만 WPF로 이동 중입니다).

건배,

토마스는

답변

0

나는 당신이 당신의 자신의 쓰기 좋을 것. 레이블이 붙은 컨트롤의 수직리스트가있는 폼의 가장 기본적인 경우, 직관적 인 접근법은 문자열 객체 쌍의 정렬 된 목록으로 구성되는 데이터 객체를 갖는 것입니다 (어쩌면 유효성 검사 규칙을 사용하여 트리플로 만들 수도 있습니다). 양식을로드해야 할 때 각 개체의 형식이 검사되고 문자열 인 경우 텍스트 상자를 만들고 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; 
} 

사용자 정의 생성자, 편집 버튼 등을 사용하면 완전히 동적 인 대화 상자로 전환 할 수 있습니다. 레이아웃이 어느 정도 정교 해지는 지에 따라 다소 효과가있을 수 있습니다. 물론 그렇게하는 라이브러리를 찾는 것이 가장 쉽습니다. 아마도 다른 사람이 알고있을 것입니다.

관련 문제