SilverLight中弹出自定义模态窗口

SilverLight中弹出自定义模态窗口

 

1.       建立Child窗口类

DemoChildWindow前台代码:

<basics:ChildWindow x:Class="System.Windows.Controls.Samples.DemoChildWindow"

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

           xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

           xmlns:dataform="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"

           Width="400" Height="300"

           Title="ChildWindowSample">

    <Grid Margin="2">

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

 

        <ContentControl HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding}" />

        <StackPanel Grid.Row="1" x:Name="optionsStack">

            <Grid HorizontalAlignment="Stretch">

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition />

                </Grid.ColumnDefinitions>

                <TextBlock Text="OverlayOpacity: " Margin="4" HorizontalAlignment="Stretch" />

                <Slider Grid.Column="1" Minimum="0" IsEnabled="True" Maximum="1" Value="{Binding OverlayOpacity, Mode=TwoWay}" HorizontalAlignment="Stretch" />

            </Grid>

            <Grid HorizontalAlignment="Stretch">

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="Auto" />

                    <ColumnDefinition />

                </Grid.ColumnDefinitions>

                <TextBlock Text="Overlay Brush: " Margin="4" />

                <ComboBox SelectedItem="{Binding OverlayBrush, Mode=TwoWay}" Grid.Column="1" IsEnabled="True" HorizontalAlignment="Stretch">

                    <ComboBox.ItemTemplate>

                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">

                                <TextBlock Margin="2" Text="{Binding Color}" FontFamily="Consolas" VerticalAlignment="Center" />

                                <Rectangle Fill="{Binding}" Margin="2" Stroke="Black" Height="14" Width="75" />

                            </StackPanel>

                        </DataTemplate>

                    </ComboBox.ItemTemplate>

                    <SolidColorBrush Color="White" Opacity=".7"  />

                    <SolidColorBrush Color="Black" Opacity=".7" />

                    <SolidColorBrush Color="Blue" Opacity=".7" />

                    <SolidColorBrush Color="Yellow" Opacity=".7" />

                    <SolidColorBrush Color="Pink" Opacity=".7" />

                    <SolidColorBrush Color="Orange" Opacity=".7" />

                    <SolidColorBrush Color="Green" Opacity=".7" />

                    <SolidColorBrush Color="Red" Opacity=".7" />

                </ComboBox>

            </Grid>

        </StackPanel>

        <Button Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="2" />

        <Button Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right"  Margin="0,12,79,0" Grid.Row="2" />

    </Grid>

</basics:ChildWindow>

DemoChildWindow后台代码

namespace System.Windows.Controls.Samples

{

    /// <summary>

    /// Sample ChildWindow for demonstration purposes.

    /// </summary>

    public partial class DemoChildWindow : ChildWindow

    {

        /// <summary>

        /// Initializes a DemoChildWindow.

        /// </summary>

        public DemoChildWindow()

        {

            InitializeComponent();

            optionsStack.DataContext = this;

        }

 

        /// <summary>

        /// Handles the Click event of the OK button.

        /// </summary>

        /// <param name="sender">OK Button.</param>

        /// <param name="e">Event arguments.</param>

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used by event defined in Xaml.")]

        private void OKButton_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult = true;

        }

 

        /// <summary>

        /// Handles the Click event of the Cancel button.

        /// </summary>

        /// <param name="sender">Cancel button.</param>

        /// <param name="e">Event arguments.</param>

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used by event defined in Xaml.")]

        private void CancelButton_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult = false;

        }

    }

}

 

2.       调用

using System.ComponentModel;

using System.Linq;

 

namespace System.Windows.Controls.Samples

{

    /// <summary>

    /// Sample page demonstrating the ChildWindow.

    /// </summary>

    [Sample("ChildWindow", DifficultyLevel.Basic)]

    [Category("ChildWindow")]

    public partial class ChildWindowSample : UserControl

    {

        /// <summary>

        /// Keeps an instance of a ChildWindow that will be shown when a button is clicked.

        /// </summary>

        private DemoChildWindow dcw;

 

        /// <summary>

        /// Initializes a new instance of the ChildWindowSample class.

        /// </summary>

        public ChildWindowSample()

        {

            InitializeComponent();

            dcw = new DemoChildWindow();

            dcw.Closed += new EventHandler(Dcw_Closed);

            thumbs.ItemsSource = from p in Photograph.GetPhotographs()

                                 orderby p.Name

                                 select p;

            thumbs.SelectedIndex = 0;

        }

 

        /// <summary>

        /// Handles the "Closed" event of the ChildWindow.

        /// </summary>

        /// <param name="sender">Child Window.</param>

        /// <param name="e">Event Arguments.</param>

        private void Dcw_Closed(object sender, EventArgs e)

        {

            dialogResult.Text = dcw.DialogResult.ToString();

        }

 

        /// <summary>

        /// Handles clicking the "Show" button.

        /// </summary>

        /// <param name="sender">Clicked Button.</param>

        /// <param name="e">Event Arguments.</param>

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used by event defined in Xaml.")]

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            dcw.Title = titleText.Text;

            dcw.DataContext = (from p in Photograph.GetPhotographs()

                               where p.Name.Equals((thumbs.SelectedItem as Photograph).Name)

                               select p).First().Image;

            dcw.Show();

        }

    }

}

 

你可能感兴趣的:(SilverLight中弹出自定义模态窗口)