Silverlight学习笔记十自定义SilverLight对话框

今天这一讲是用SilverLight作的自定义对话框(提示、警告、错误)

效果如图

 

一、定义俩个枚举类型BoxButton、MessageBoxType

1.BoxButton.cs它的目的在于指示按钮的类型。

 /// <summary>
    /// 按钮类型
    /// </summary>
    public enum BoxButton
    {
        /// <summary>
        /// 关闭按钮
        /// </summary>
        OK = 0,

        /// <summary>
        /// 确定、取消按钮
        /// </summary>
        OKCancel = 1,

        /// <summary>
        /// 是、否按钮
        /// </summary>
        YesOrNo = 2
    }

 

2.MessageBoxType.cs是对话框的类型(提示,错误,警告)

 /// <summary>
    /// 对话框类型
    /// </summary>
    public enum MessageBoxType
    {
        /// <summary>
        /// 错误对话框
        /// </summary>
        Error = 0,

        /// <summary>
        /// 警告对话框
        /// </summary>
        Warnnig = 1,

        /// <summary>
        /// 提示对话框
        /// </summary>
        Info = 2
    }

 

二、窗体程序

1.SLMessageBox.xaml

<controls:ChildWindow x:Class="Silverlight.Common.View.SLMessageBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           MinWidth="200" MinHeight="120" 
           Title="提示" >

    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition  Height="6"/>
            <RowDefinition  Height="30"/>
            <RowDefinition  Height="6"/>
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition  Width="90"/>

            <ColumnDefinition Width="90" />
        </Grid.ColumnDefinitions>
        <Image Grid.Row="1" Width="16" Height="16"  Grid.Column="0" HorizontalAlignment="Left"  x:Name="msgPic"></Image>
        <TextBlock Grid.Row="1" Grid.ColumnSpan="2" x:Name="message" HorizontalAlignment="Center" VerticalAlignment="Center"/>

        <Button x:Name="btnOK"  Content="确定" Click="btnOK_Click"  Width="40" Height="23"  VerticalAlignment="Center" HorizontalAlignment="Center"   Grid.Row="23"  Grid.Column="0" HorizontalContentAlignment="Center"/>
        <Button x:Name="btnCancel"  Content="取消" Click="btnCancel_Click"  Width="40" Height="23"  VerticalAlignment="Center" HorizontalAlignment="Center"   Grid.Row="3"  Grid.Column="1"  HorizontalContentAlignment="Center"/>
        <Button x:Name="btnClose"  Content="关闭" Click="btnClose_Click"  Width="40" Height="23"  VerticalAlignment="Center" HorizontalAlignment="Center"   Grid.Row="3"  Grid.ColumnSpan="2" Visibility="Collapsed"  HorizontalContentAlignment="Center"/>

    </Grid>
</controls:ChildWindow>

2.SLMessageBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using Silverlight.Common.Core;

namespace Silverlight.Common.View
{
    public partial class SLMessageBox : ChildWindow
    {
        public SLMessageBox()
        {
            InitializeComponent();

    this.msgPic.Source = new BitmapImage(new Uri("http://images.cnblogs.com/info.jpg", UriKind.Relative));
        }

        /// <summary>
        /// 委托
        /// </summary>
        private Action showDialogHandler;

        #region 简单对话框
        /// <summary>
        /// 确定、取消对话框
        /// </summary>
        /// <param name="showDialogHandler">点击确定后要执行的方法</param>
        /// <param name="msg">信息</param>
        /// <param name="caption">对话框标题</param>
        public void showDialog(Action showDialogHandler, string msg, string caption)
        {
            this.message.Text = msg;
            if (!string.IsNullOrEmpty(caption))
            {
                this.Title = caption;
            }

            this.showDialogHandler = showDialogHandler;
            this.Show();
        }

        #endregion

        #region 对话框

        /// <summary>
        ///显示对话框
        /// </summary>
        /// <param name="showDialogHandler">点击“确定”按钮或“是”按钮后要执行的方法</param>
        /// <param name="msgType">对话框类型(16×16)</param>
        /// <param name="msg">显示的信息</param>
        /// <param name="caption">对话框标题</param>
        /// <param name="button">按钮样式</param>
        public void showDialog(Action showDialogHandler, string msg, MessageBoxType msgType, string caption, BoxButton button)
        {
            #region 信息

            this.message.Text = msg;

            #endregion

            #region 对话框标题

            if (!string.IsNullOrEmpty(caption))
            {
                this.Title = caption;
            }

            #endregion

            #region 图片样式

            switch (msgType)
            {
                case MessageBoxType.Error:
                    this.msgPic.Source = new BitmapImage(new Uri("../images/error.jpg", UriKind.Relative));
                    break;

                case MessageBoxType.Warnnig:
                    this.msgPic.Source = new BitmapImage(new Uri("../images/warning.jpg", UriKind.Relative));
                    break;

                case MessageBoxType.Info:
                    this.msgPic.Source = new BitmapImage(new Uri("../images/info.jpg", UriKind.Relative));
                    break;
            }

            #endregion

            #region 按钮样式

            switch (button)
            {
                case BoxButton.OK:
                    this.btnCancel.Visibility = Visibility.Collapsed;
                    this.btnOK.Visibility = Visibility.Collapsed;
                    this.btnClose.Visibility = Visibility.Visible;
                    break;

                case BoxButton.OKCancel:
                    this.btnCancel.Visibility = Visibility.Visible;
                    this.btnOK.Visibility = Visibility.Visible;
                    this.btnClose.Visibility = Visibility.Collapsed;
                    this.showDialogHandler = showDialogHandler;
                    break;

                case BoxButton.YesOrNo:
                    this.btnCancel.Content = "否";
                    this.btnOK.Content = "是";
                    this.btnCancel.Visibility = Visibility.Visible;
                    this.btnOK.Visibility = Visibility.Visible;
                    this.btnClose.Visibility = Visibility.Collapsed;
                    this.showDialogHandler = showDialogHandler;
                    break;
            }

            #endregion

            this.Show();
        }

        #endregion

        #region 按钮事件

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            if (this.showDialogHandler != null)
            {
                this.showDialogHandler();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}

 

3.是一个DataGrid用来演示效果的,前面已经介绍过了,在http://www.cnblogs.com/salam/archive/2010/07/12/1775891.html地址,本节中只是在

  <datagrid:DataGrid x:Name="dataGrid"  Width="820" Height="310" ColumnWidth="100"  ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="4" MaxHeight="300" Grid.Column="1">中增加了右键菜单,用来演示自定义对话框。
                <input:ContextMenuService.ContextMenu>
                    <input:ContextMenu>
                        <input:MenuItem Header="分组显示" x:Name="cmiGrouping" Click="cmiGrouping_Click">
                            <input:MenuItem.Icon>
                                <Image Source="../Images/File.png"/>
                            </input:MenuItem.Icon>
                        </input:MenuItem>

                        <input:MenuItem Header="只读" x:Name="cmiOrder" Click="cmiOrder_Click">
                            <input:MenuItem.Icon>
                                <Image Source="../Images/key.png"/>
                            </input:MenuItem.Icon>
                        </input:MenuItem>

                        <input:MenuItem Header="删除" x:Name="cmiDelete" Click="cmiDelete_Click">
                            <input:MenuItem.Icon>
                                <Image Source="../Images/ButtonSaveAndClose.png"/>
                            </input:MenuItem.Icon>
                        </input:MenuItem>
                       
                    </input:ContextMenu>
                </input:ContextMenuService.ContextMenu>
                <datagrid:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <StackPanel Background="LightBlue">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="This item has details." />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Here is some data: " />
                                <TextBlock Text="{Binding Name}" />
                                <TextBlock Text=" " />
                                <TextBlock Text="{Binding Password}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </datagrid:DataGrid.RowDetailsTemplate>
            </datagrid:DataGrid>

 

其它的就不多说了在http://www.cnblogs.com/salam/archive/2010/07/12/1775891.html已写啦。呵呵!本人比较懒

在这顺便说一下,如果能在程序中用枚举,还是不错的,我鼓励大家试着用。由于本人也是初学,所以大家不要见笑啊。

源代码地址:https://files.cnblogs.com/salam/Silverlight.Common.rar

 

 

 

posted @ 2010-07-14 01:36  ForrestWoo  阅读(1872)  评论(1编辑  收藏  举报