MessageBox
메시지 상자가 텍스트 정보를 표시하고 사용자가 버튼을 사용하여 의사 결정을 내릴 수 있도록하는 데 사용할 수있는 대화 상자입니다.
오버라이드 된 정적 메서드 Show()에 매개변수를 설정하고 호출하여 사용자에게 문자 메시지를 표시하는 미리 만들어진 모달 대화 상자 예제 입니다.
xaml
2개의 열과 9개의 행으로 구성된 Grid에 메시지 박스 관련 옵션을 구성 합니다.
<Window x:Class="MessageBox.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"
xmlns:local="clr-namespace:MessageBox"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0">Associate with Owner Window?</Label>
<CheckBox Grid.Column="1" Grid.Row="0" Name="ownerCheckBox" />
<Label Grid.Column="0" Grid.Row="1">messageBoxText:</Label>
<TextBox Grid.Column="1" Grid.Row="1" Name="messageBoxText">MessageBoxText</TextBox>
<Label Grid.Column="0" Grid.Row="2">caption:</Label>
<TextBox Grid.Column="1" Grid.Row="2" Name="caption">Caption</TextBox>
<Label Grid.Column="0" Grid.Row="3">button:</Label>
<ComboBox Grid.Column="1" Grid.Row="3" Name="buttonComboBox">
<ComboBoxItem IsSelected="True">OK</ComboBoxItem>
<ComboBoxItem>OKCancel</ComboBoxItem>
<ComboBoxItem>YesNo</ComboBoxItem>
<ComboBoxItem>YesNoCancel</ComboBoxItem>
</ComboBox>
<Label Grid.Column="0" Grid.Row="4">icon:</Label>
<ComboBox Grid.Column="1" Grid.Row="4" Name="imageComboBox">
<ComboBoxItem>Asterisk</ComboBoxItem>
<ComboBoxItem>Error</ComboBoxItem>
<ComboBoxItem>Exclamation</ComboBoxItem>
<ComboBoxItem>Hand</ComboBoxItem>
<ComboBoxItem>Information</ComboBoxItem>
<ComboBoxItem IsSelected="True">None</ComboBoxItem>
<ComboBoxItem>Question</ComboBoxItem>
<ComboBoxItem>Stop</ComboBoxItem>
<ComboBoxItem>Warning</ComboBoxItem>
</ComboBox>
<Label Grid.Column="0" Grid.Row="5">defaultResult:</Label>
<ComboBox Grid.Column="1" Grid.Row="5" Name="defaultResultComboBox">
<ComboBoxItem>Cancel</ComboBoxItem>
<ComboBoxItem>No</ComboBoxItem>
<ComboBoxItem IsSelected="True">None</ComboBoxItem>
<ComboBoxItem>OK</ComboBoxItem>
<ComboBoxItem>Yes</ComboBoxItem>
</ComboBox>
<Label Grid.Column="0" Grid.Row="6">options</Label>
<ComboBox Grid.Column="1" Grid.Row="6" Name="optionsComboBox">
<ComboBoxItem>DefaultDesktopOnly</ComboBoxItem>
<ComboBoxItem IsSelected="True">None</ComboBoxItem>
<ComboBoxItem>RightAlign</ComboBoxItem>
<ComboBoxItem>RtlReading</ComboBoxItem>
<ComboBoxItem>ServiceNotification</ComboBoxItem>
</ComboBox>
<Button Grid.Column="1" Grid.Row="7" Name="showMessageBoxButton" Click="ShowMessageBoxButton_Click">Show MessageBox</Button>
<StatusBar Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="8" >
<StatusBarItem>
<TextBlock Name="resultTextBlock">Ready</TextBlock>
</StatusBarItem>
</StatusBar>
</Grid>
</Window>
code
UI에서 설정한 값을 이용해 메시지 박스를 출력 합니다.
private void ShowMessageBoxButton_Click(object sender, RoutedEventArgs e)
{
Window owner = ((bool)ownerCheckBox.IsChecked ? this : null);
var messageBoxText = this.messageBoxText.Text;
var caption = this.caption.Text;
var button = (MessageBoxButton)Enum.Parse(typeof(MessageBoxButton), buttonComboBox.Text);
var icon = (MessageBoxImage)Enum.Parse(typeof(MessageBoxImage), imageComboBox.Text);
var defaultResult = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), defaultResultComboBox.Text);
var options = (MessageBoxOptions)Enum.Parse(typeof(MessageBoxOptions), optionsComboBox.Text);
//
MessageBoxResult result;
if (owner == null)
{
result = System.Windows.MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
}
else
{
result = System.Windows.MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
}
resultTextBlock.Text = "Result = " + result;
}
댓글
댓글 쓰기