ModalWindow.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media.Animation;
  5. namespace MediaBrowser.UI.Controls
  6. {
  7. /// <summary>
  8. /// Interaction logic for ModalWindow.xaml
  9. /// </summary>
  10. public partial class ModalWindow : BaseModalWindow
  11. {
  12. public MessageBoxResult MessageBoxResult { get; set; }
  13. public UIElement TextContent
  14. {
  15. set
  16. {
  17. pnlContent.Children.Clear();
  18. var textBlock = value as TextBlock;
  19. if (textBlock != null)
  20. {
  21. textBlock.SetResourceReference(TextBlock.StyleProperty, "ModalTextStyle");
  22. }
  23. pnlContent.Children.Add(value);
  24. }
  25. }
  26. public string Text
  27. {
  28. set { TextContent = new TextBlock { Text = value }; }
  29. }
  30. private MessageBoxButton _button;
  31. public MessageBoxButton Button
  32. {
  33. get { return _button; }
  34. set
  35. {
  36. _button = value;
  37. UpdateButtonVisibility();
  38. OnPropertyChanged("Button");
  39. }
  40. }
  41. private MessageBoxIcon _messageBoxImage;
  42. public MessageBoxIcon MessageBoxImage
  43. {
  44. get { return _messageBoxImage; }
  45. set
  46. {
  47. _messageBoxImage = value;
  48. OnPropertyChanged("MessageBoxImage");
  49. }
  50. }
  51. private string _caption;
  52. public string Caption
  53. {
  54. get { return _caption; }
  55. set
  56. {
  57. _caption = value;
  58. txtCaption.Visibility = string.IsNullOrEmpty(value) ? Visibility.Collapsed : Visibility.Visible;
  59. OnPropertyChanged("Caption");
  60. }
  61. }
  62. public ModalWindow()
  63. : base()
  64. {
  65. InitializeComponent();
  66. }
  67. protected override void OnInitialized(EventArgs e)
  68. {
  69. base.OnInitialized(e);
  70. btnOk.Click += btnOk_Click;
  71. btnCancel.Click += btnCancel_Click;
  72. btnYes.Click += btnYes_Click;
  73. btnNo.Click += btnNo_Click;
  74. }
  75. void btnNo_Click(object sender, RoutedEventArgs e)
  76. {
  77. MessageBoxResult = MessageBoxResult.No;
  78. CloseModal();
  79. }
  80. void btnYes_Click(object sender, RoutedEventArgs e)
  81. {
  82. MessageBoxResult = MessageBoxResult.Yes;
  83. CloseModal();
  84. }
  85. void btnCancel_Click(object sender, RoutedEventArgs e)
  86. {
  87. MessageBoxResult = MessageBoxResult.Cancel;
  88. CloseModal();
  89. }
  90. void btnOk_Click(object sender, RoutedEventArgs e)
  91. {
  92. MessageBoxResult = MessageBoxResult.OK;
  93. CloseModal();
  94. }
  95. private void UpdateButtonVisibility()
  96. {
  97. btnYes.Visibility = Button == MessageBoxButton.YesNo || Button == MessageBoxButton.YesNoCancel
  98. ? Visibility.Visible
  99. : Visibility.Collapsed;
  100. btnNo.Visibility = Button == MessageBoxButton.YesNo || Button == MessageBoxButton.YesNoCancel
  101. ? Visibility.Visible
  102. : Visibility.Collapsed;
  103. btnOk.Visibility = Button == MessageBoxButton.OK || Button == MessageBoxButton.OKCancel
  104. ? Visibility.Visible
  105. : Visibility.Collapsed;
  106. btnCancel.Visibility = Button == MessageBoxButton.OKCancel || Button == MessageBoxButton.YesNoCancel
  107. ? Visibility.Visible
  108. : Visibility.Collapsed;
  109. }
  110. }
  111. /// <summary>
  112. /// I had to make my own enum that essentially clones MessageBoxImage
  113. /// Some of the options share the same enum int value, and this was preventing databinding from working properly.
  114. /// </summary>
  115. public enum MessageBoxIcon
  116. {
  117. // Summary:
  118. // No icon is displayed.
  119. None,
  120. //
  121. // Summary:
  122. // The message box contains a symbol consisting of white X in a circle with
  123. // a red background.
  124. Error,
  125. //
  126. // Summary:
  127. // The message box contains a symbol consisting of a white X in a circle with
  128. // a red background.
  129. Hand,
  130. //
  131. // Summary:
  132. // The message box contains a symbol consisting of white X in a circle with
  133. // a red background.
  134. Stop,
  135. //
  136. // Summary:
  137. // The message box contains a symbol consisting of a question mark in a circle.
  138. Question,
  139. //
  140. // Summary:
  141. // The message box contains a symbol consisting of an exclamation point in a
  142. // triangle with a yellow background.
  143. Exclamation,
  144. //
  145. // Summary:
  146. // The message box contains a symbol consisting of an exclamation point in a
  147. // triangle with a yellow background.
  148. Warning,
  149. //
  150. // Summary:
  151. // The message box contains a symbol consisting of a lowercase letter i in a
  152. // circle.
  153. Information,
  154. //
  155. // Summary:
  156. // The message box contains a symbol consisting of a lowercase letter i in a
  157. // circle.
  158. Asterisk
  159. }
  160. }