WPF: 대화 상자/프롬프트 생성
사용자 입력을 위해 TextBox를 포함한 Dialog / Prompt를 작성해야 합니다.문제는 대화 내용을 확인한 후 어떻게 문자를 받을 수 있는가 하는 것입니다.보통 나는 이것을 위한 클래스를 만들어서 텍스트를 속성에 저장합니다.그러나 XAML을 사용하여 대화상자를 설계하고 싶습니다.그래서 어떻게든 XAML 코드를 확장하여 TextBox의 콘텐츠를 속성에 저장해야 합니다.그런데 순수 XAML로는 불가능할 것 같습니다.제가 하고 싶은 것을 실현하는 가장 좋은 방법은 무엇일까요?XAML에서 정의할 수 있지만 어떻게든 입력을 반환할 수 있는 대화상자를 구축하려면 어떻게 해야 합니까?힌트 고마워요!
"책임 있는" 답변은 대화 상자에 View Model을 구축하고 View Model이 "ResponseText" 속성 등을 가질 수 있도록 TextBox에서 양방향 데이터 바인딩을 사용하는 것입니다.이것은 충분히 하기 쉽지만 아마도 과잉 살상일 것이다.
실용적인 대답은 텍스트 상자에 x:Name을 지정하여 멤버가 되고 다음과 같이 클래스 뒤에서 코드의 속성으로 텍스트를 노출하는 것입니다.
<!-- Incredibly simplified XAML -->
<Window x:Class="MyDialog">
<StackPanel>
<TextBlock Text="Enter some text" />
<TextBox x:Name="ResponseTextBox" />
<Button Content="OK" Click="OKButton_Click" />
</StackPanel>
</Window>
그럼 뒤에 있는 네 암호는...
partial class MyDialog : Window {
public MyDialog() {
InitializeComponent();
}
public string ResponseText {
get { return ResponseTextBox.Text; }
set { ResponseTextBox.Text = value; }
}
private void OKButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
DialogResult = true;
}
}
그럼 그걸 쓰려면...
var dialog = new MyDialog();
if (dialog.ShowDialog() == true) {
MessageBox.Show("You said: " + dialog.ResponseText);
}
편집: nuget https://www.nuget.org/packages/PromptDialog/과 함께 설치할 수 있습니다.
MessageBox와 같은 정적 메서드를 추가합니다.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Class="utils.PromptDialog"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
SizeToContent="WidthAndHeight"
MinWidth="300"
MinHeight="100"
WindowStyle="SingleBorderWindow"
ResizeMode="CanMinimize">
<StackPanel Margin="5">
<TextBlock Name="txtQuestion" Margin="5"/>
<TextBox Name="txtResponse" Margin="5"/>
<PasswordBox Name="txtPasswordResponse" />
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right">
<Button Content="_Ok" IsDefault="True" Margin="5" Name="btnOk" Click="btnOk_Click" />
<Button Content="_Cancel" IsCancel="True" Margin="5" Name="btnCancel" Click="btnCancel_Click" />
</StackPanel>
</StackPanel>
</Window>
그리고 뒤에 있는 코드:
public partial class PromptDialog : Window
{
public enum InputType
{
Text,
Password
}
private InputType _inputType = InputType.Text;
public PromptDialog(string question, string title, string defaultValue = "", InputType inputType = InputType.Text)
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(PromptDialog_Loaded);
txtQuestion.Text = question;
Title = title;
txtResponse.Text = defaultValue;
_inputType = inputType;
if (_inputType == InputType.Password)
txtResponse.Visibility = Visibility.Collapsed;
else
txtPasswordResponse.Visibility = Visibility.Collapsed;
}
void PromptDialog_Loaded(object sender, RoutedEventArgs e)
{
if (_inputType == InputType.Password)
txtPasswordResponse.Focus();
else
txtResponse.Focus();
}
public static string Prompt(string question, string title, string defaultValue = "", InputType inputType = InputType.Text)
{
PromptDialog inst = new PromptDialog(question, title, defaultValue, inputType);
inst.ShowDialog();
if (inst.DialogResult == true)
return inst.ResponseText;
return null;
}
public string ResponseText
{
get
{
if (_inputType == InputType.Password)
return txtPasswordResponse.Password;
else
return txtResponse.Text;
}
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
그래서 다음과 같이 부를 수 있습니다.
string repeatPassword = PromptDialog.Prompt("Repeat password", "Password confirm", inputType: PromptDialog.InputType.Password);
조쉬의 훌륭한 답변입니다. 모두 조쉬 덕분입니다. 하지만 저는 약간 수정했습니다.
MyDialog Xaml
<StackPanel Margin="5,5,5,5">
<TextBlock Name="TitleTextBox" Margin="0,0,0,10" />
<TextBox Name="InputTextBox" Padding="3,3,3,3" />
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="BtnOk" Content="OK" Grid.Column="0" Margin="0,0,5,0" Padding="8" Click="BtnOk_Click" />
<Button Name="BtnCancel" Content="Cancel" Grid.Column="1" Margin="5,0,0,0" Padding="8" Click="BtnCancel_Click" />
</Grid>
</StackPanel>
MyDialog 코드 이면
public MyDialog()
{
InitializeComponent();
}
public MyDialog(string title,string input)
{
InitializeComponent();
TitleText = title;
InputText = input;
}
public string TitleText
{
get { return TitleTextBox.Text; }
set { TitleTextBox.Text = value; }
}
public string InputText
{
get { return InputTextBox.Text; }
set { InputTextBox.Text = value; }
}
public bool Canceled { get; set; }
private void BtnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
{
Canceled = true;
Close();
}
private void BtnOk_Click(object sender, System.Windows.RoutedEventArgs e)
{
Canceled = false;
Close();
}
그리고 다른 곳으로 불러봐
var dialog = new MyDialog("test", "hello");
dialog.Show();
dialog.Closing += (sender,e) =>
{
var d = sender as MyDialog;
if(!d.Canceled)
MessageBox.Show(d.InputText);
}
당신은 다른 어떤 멋진 대답도 필요하지 않습니다.다음 예시는 단순한 예시로, 모든 것을 포함하지는 않습니다.Margin,Height,Width기본 수준에서 이 작업을 수행하는 방법을 보여주기에 충분합니다.
XAML
의 빌드Window정상적으로 페이지를 작성하여 필드에 추가합니다.예를 들어, 예를 들어,Label그리고.TextBox내부의 제어StackPanel:
<StackPanel Orientation="Horizontal">
<Label Name="lblUser" Content="User Name:" />
<TextBox Name="txtUser" />
</StackPanel>
그런 다음 표준을 만듭니다.Button제출("OK" 또는 "Submit") 및 "Cancel" 버튼을 클릭합니다.
<StackPanel Orientation="Horizontal">
<Button Name="btnSubmit" Click="btnSubmit_Click" Content="Submit" />
<Button Name="btnCancel" Click="btnCancel_Click" Content="Cancel" />
</StackPanel>
코드 비하인드
를 추가합니다.Click이벤트 핸들러는 코드 배후에 기능합니다만, 거기에 액세스 할 때는, 우선, 텍스트 박스 값을 격납하는 퍼블릭 변수를 선언해 주세요.
public static string strUserName = String.Empty;
그런 다음 이벤트 핸들러 기능의 경우(ClickXAML 버튼에서 기능을 수행하고 "정의로 이동"을 선택합니다. 그러면 해당 정의가 생성됩니다.) 상자가 비어 있는지 확인해야 합니다.변수가 아닌 경우 변수에 저장하고 창을 닫습니다.
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtUser.Text))
{
strUserName = txtUser.Text;
this.Close();
}
else
MessageBox.Show("Must provide a user name in the textbox.");
}
Page( 페이지에서 호출)this.Close()저 위에서는 제 가치가 사라졌죠? 안 돼요!!다른 사이트 http://www.dreamincode.net/forums/topic/359208-wpf-how-to-make-simple-popup-window-for-input/에서 이 사실을 알게 되었습니다.
(치웠는데) 법 게 요.Window다음 값을 가져옵니다.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenPopup_Click(object sender, RoutedEventArgs e)
{
MyPopupWindow popup = new MyPopupWindow(); // this is the class of your other page
//ShowDialog means you can't focus the parent window, only the popup
popup.ShowDialog(); //execution will block here in this method until the popup closes
string result = popup.strUserName;
UserNameTextBlock.Text = result; // should show what was input on the other page
}
}
[ ]버튼 [ Cancel ]단 。
' 버튼은 아, 아, 아, 아, 아, 아, 아, 아, 아, 아, 네?따라서 팝업창 코드 뒤에 다른 공개변수를 추가합니다.
public static bool cancelled = false;
그리고 우리 회사도 포함시켜보자.btnCancel_Click핸들러로 해 주세요.btnSubmit_Click:
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
cancelled = true;
strUserName = String.Empty;
this.Close();
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtUser.Text))
{
strUserName = txtUser.Text;
cancelled = false; // <-- I add this in here, just in case
this.Close();
}
else
MessageBox.Show("Must provide a user name in the textbox.");
}
그 .MainWindow btnOpenPopup_Click 이벤트 표시:
private void btnOpenPopup_Click(object sender, RoutedEventArgs e)
{
MyPopupWindow popup = new MyPopupWindow(); // this is the class of your other page
//ShowDialog means you can't focus the parent window, only the popup
popup.ShowDialog(); //execution will block here in this method until the popup closes
// **Here we find out if we cancelled or not**
if (popup.cancelled == true)
return;
else
{
string result = popup.strUserName;
UserNameTextBlock.Text = result; // should show what was input on the other page
}
}
이지만, 이 싶었어요.public static안 돼, 안 돼.DialogResult반환되는 값 없음, 아무것도 없습니다.창을 열고 팝업창에서 버튼 이벤트와 함께 값을 저장한 후 메인창 기능에서 검색하기만 하면 됩니다.
언급URL : https://stackoverflow.com/questions/2796470/wpf-create-a-dialog-prompt
'programing' 카테고리의 다른 글
| Excel의 mod 함수와 동등한 VBA (0) | 2023.04.19 |
|---|---|
| UTF8 데이터를 Excel로 내보내는 가장 좋은 방법은 무엇입니까? (0) | 2023.04.14 |
| 파워셸에서 머리, 꼬리, 더, 덜, 더, 덜, 더, 더, 더, 더, 더, 더, 더, 더, 더, 더, 더, 어떻게 하는 (0) | 2023.04.14 |
| SQL Server - 클러스터된 인덱스와 비클러스터된 인덱스를 사용할 때 (0) | 2023.04.14 |
| Excel에서 피벗 테이블 데이터 소스를 변경하는 방법 (0) | 2023.04.14 |