WPF彈出帶蒙板的音訊框。本站提示廣大學習愛好者:(WPF彈出帶蒙板的音訊框)文章只能為提供參考,不一定能成為您想要的結果。以下是WPF彈出帶蒙板的音訊框正文
先看看效果圖

思緒
拿到父級窗體的內容,放入一個容器裡,再在容器裡放入一個半通明層.將整個容器賦給父級窗體的內容.

封閉時反向操作.

代碼
音訊窗彈出時
/// <summary>
/// 彈出音訊框
/// </summary>
/// <param name="message">音訊</param>
/// <param name="owner">父級窗體</param>
public static void ShowDialog(string message, Window owner)
{
//蒙板
Grid layer = new Grid() { Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)) };
//父級窗體原來的內容
UIElement original = owner.Content as UIElement;
owner.Content = null;
//容器Grid
Grid container = new Grid();
container.Children.Add(original);//放入原來的內容
container.Children.Add(layer);//在下面放一層蒙板
//將裝有原來內容和蒙板的容器賦給父級窗體
owner.Content = container;
//彈出音訊框
MessageBox box = new MessageBox() { Owner = owner };
box.tbc_message.Text = message;
box.ShowDialog();
}
音訊框封閉時
/// <summary>
/// 窗體封閉事情
/// </summary>
private void Window_Closed(object sender, EventArgs e)
{
//容器Grid
Grid grid = this.Owner.Content as Grid;
//父級窗體原來的內容
UIElement original = VisualTreeHelper.GetChild(grid, 0) as UIElement;
//將父級窗體原來的內容在容器Grid中移除
grid.Children.Remove(original);
//賦給父級窗體
this.Owner.Content = original;
}
源碼下載:http://xiazai.jb51.net/201612/yuanma/MessageBox(jb51.net).rar
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。