微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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" Grid.Row="2" />

        <Button Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right"  Margin="0,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",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",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();

        }

    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐