programing

WPF: 동적으로 작성된 WPF 창에서 컨트롤을 동적으로 추가하는 방법

newnotes 2023. 4. 20. 22:42
반응형

WPF: 동적으로 작성된 WPF 창에서 컨트롤을 동적으로 추가하는 방법

C#의 프로젝트에 WPF 입력 박스를 추가하고 싶습니다.C#에 있는 InputBox에서 WinForm을 샀는데 WinForm 룩앤필이 들어가 있어요.그래서 나는 그것을 WPF로 재현하고 있었다.모든 컨트롤(라벨, 버튼, 텍스트 상자)을 작성했지만 창에 추가할 수 없습니다.

static Window winInputDialog

창은 ShowDialog를 통해 표시되지만 컨트롤은 없습니다.

창에서 컨트롤을 가져오려면 다음 두 가지 방법이 있습니다.

  1. Visual Studio 디자이너에서 전체 디자인 작업을 수행합니다.
  2. 코드별로 컨트롤을 추가합니다.다음은 창을 만들고 창을 제어하는 간단한 예입니다.

    var window = new Window();
    var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
    stackPanel.Children.Add(new Label { Content = "Label" });
    stackPanel.Children.Add(new Button { Content = "Button" });
    window.Content = stackPanel;
    

언급URL : https://stackoverflow.com/questions/7884185/wpf-how-to-dynamically-add-controls-in-dynamically-created-wpf-window

반응형