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

SaveFileDialog, HeaderedItemsControl, VirtualizingStackPanel

SaveFileDialog,HeaderedItemsControl,VirtualizingStackPanel

介绍
Silverlight 3.0 
控件一览:

·         ChildWindow - 用于在父窗体前弹出一个的子窗体

·         SaveFileDialog - 用户发起的保存文件对话框(OpenFileDialog - 打开文件对话框)

·         HeaderedItemsControl - 呈现标题和集合数据的控件

·         VirtualizingStackPanel - 虚拟化的 StackPanel(即仅生成需要显示 UI 元素。当绑定了大量数据,而某时仅显示其中一小部分的时候,使用此控件则可大幅提高呈现效率) 


示例
1
、演示 ChildWindow 的应用
ChildWindowDemo.xaml

<navigation:Page x:Class="Silverlight30.Control.ChildWindowDemo" 
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable
="d"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth
="640" d:DesignHeight="480"
           Title
="ChildWindowDemo Page">
    
<Grid x:Name="LayoutRoot">
        
<StackPanel>

            
<Button x:Name="btnChildWindow" Content="Show ChildWindow" Click="btnChildWindow_Click" />
            
<Button x:Name="btnCustomChildWindow" Content="Show CustomChildWindow" Click="btnCustomChildWindow_Click" />
    
            
<TextBlock x:Name="lblResult" />
            
        
</StackPanel>
    
</Grid>
</navigation:Page>

 
ChildWindowDemo.xaml.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.Navigation;

namespace Silverlight30.Control
@H_834_404@{
    
public partial class ChildWindowDemo : Page
    
@H_834_404@ {
        
public ChildWindowDemo()
        
@H_834_404@ {
            InitializeComponent();
        }

        
private void btnChildWindow_Click(object sender, RoutedEventArgs e)
        
@H_834_404@ {
            
/**//*
             * ChildWindow - 
在父窗体前显示的子窗体
             *     Title - 
子窗体的标题
             *     Content - 
子窗体的内容
             *     HasCloseButton - 
子窗体上是否要有关闭按钮(右上角的 ×
             *     OverlayBrush - 
子窗体打开后,覆盖在父窗体上的 Brush
             *     OverlayOpacity - 
子窗体打开后,覆盖在父窗体上的 Brush 的不透明度
             *     Width - 
子窗体的宽
             *     Height - 
子窗体的高
             *     Closed
事件 - 子窗体关闭后所触发的事件
             *     Show() - 
打开(显示)子窗体
             */


            ChildWindow child = 
new ChildWindow();
            child.Title = 
"标题";
            child.Content = 
"内容";
            child.HasCloseButton = 
true;
            child.OverlayBrush = 
new SolidColorBrush(Colors.Red);
            child.OverlayOpacity = 
0.3;
            child.Width = 
320;
            child.Height = 
240;
            
            child.Show();
        }

        
void child_Closed(object sender, EventArgs e)
        
@H_834_404@ {
            
/**//*
             * ChildWindow.DialogResult - 
子窗体传递回来的一个 bool? 值(可以用来描述在子窗体中是单击了确定按钮还是取消按钮)
             * ChildWindow.DataContext - 
子窗体传递回来的数据上下文
             */


            CustomChildWindow child = sender 
as CustomChildWindow;
            MessageBox.Show(
string.Format("DialogResult:{0}; DataContext:{1}", child.DialogResult, child.DataContext));
        }

        
private void btnCustomChildWindow_Click(object sender, RoutedEventArgs e)
        
@H_834_404@ {
            CustomChildWindow child = 
new CustomChildWindow();
            child.Closed += 
new EventHandler(child_Closed);
            child.Show();
        }
    }
}


CustomChildWindow.xaml
自定义子窗体)

<controls:ChildWindow x:Class="Silverlight30.Control.CustomChildWindow"
           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"
           Width
="320" Height="240" 
           Title
="我是标题">
    
<Grid x:Name="LayoutRoot" Margin="2">
        
<Grid.RowDeFinitions>
            
<RowDeFinitio/>
            
<RowDeFinitioHeight="Auto" />
        
</Grid.RowDeFinitions>

        
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0" Grid.Row="1" />
        
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,79,0" Grid.Row="1" />
    
</Grid>
</controls:ChildWindow>


CustomChildWindow.xaml.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;

namespace Silverlight30.Control
@H_834_404@{
    
public partial class CustomChildWindow : System.Windows.Controls.ChildWindow
    
@H_834_404@ {
        
public CustomChildWindow()
        
@H_834_404@ {
            InitializeComponent();
        }

        
private void OKButton_Click(object sender, RoutedEventArgs e)
        
@H_834_404@ {
            
/**//*
             * ChildWindow.DialogResult - 
传递给父窗体的一个 bool? 值(可以用来描述在子窗体中是单击了确定按钮还是取消按钮)
             * ChildWindow.DataContext - 
传递给父窗体的数据上下文
             */


            
this.DataContext = "点击了 OK 按钮";
            
this.DialogResult = true;
        }

        
private void CancelButton_Click(object sender, RoutedEventArgs e)
        
@H_834_404@ {
            
this.DataContext = "点击了 Cancel 按钮";
            
this.DialogResult = false;
        }
    }
}



2
SaveFileDialog OpenFileDialog 的演示
SaveFileDialogDemo.xaml

<navigation:Page x:Class="Silverlight30.Control.SaveFileDialogDemo" 
           xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable
="d"
           xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth
="640" d:DesignHeight="480"
           Title
="SaveFileDialog Page">
    
<Grid x:Name="LayoutRoot">
        
<StackPanel>
        
            
<TextBox x:Name="txtInfo" />
            
<Button x:Name="btnSave" Content="保存" Click="btnSave_Click" />
            
<Button x:Name="btnLoad" Content="载入" Click="btnLoad_Click" />
            
        
</StackPanel>
    
</Grid>
</navigation:Page>


SaveFileDialogDemo.xaml.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.Navigation;

using System.IO;
using System.Text;

namespace Silverlight30.Control
@H_834_404@{
    
public partial class SaveFileDialogDemo : Page
    
@H_834_404@ {
        
public SaveFileDialogDemo()
        
@H_834_404@ {
            InitializeComponent();
        }

        
private void btnSave_Click(object sender, RoutedEventArgs e)
        
@H_834_404@ {
            
/**//*
             * SaveFileDialog - 
用户发起的保存文件对话框
             *     Filter - 
指定保存文件的描述信息及文件类型(出现在对话框的保存类型下拉列表中)
             *     DefaultExt - 
当指定保存文件类型为 *.* 时的认扩展名
             *     FilterIndex - 
认的保存类型在 Filter 中的索引(注意:索引从 1 开始)
             *     ShowDialog() - 
显示保存文件对话框。用户在对话框中单击保存则返回 true;单击取消关闭对话框则返回 false
             *     OpenFile() - 
打开用户选择的文件,并返回文件
             */


            SaveFileDialog dialog = 
new SaveFileDialog();
            dialog.Filter = 
"Text Files|*.txt|Log Files|*.log|All Files|*.*";
            dialog.FilterIndex = 
1;

            
bool? result = dialog.ShowDialog();
            
if (result == true)
            
@H_834_404@ {
                
using (Stream stream = dialog.OpenFile())
                
@H_834_404@ {
                    
byte[] info = Encoding.UTF8.GetBytes(txtInfo.Text);
                    stream.Write(info, 
0, info.Length);
                }

                txtInfo.Text = 
"";
            }
        }

        
private void btnLoad_Click(object sender, RoutedEventArgs e)
        
@H_834_404@ {
            
/**//*
             * OpenFileDialog - 
打开文件对话框
             *     Filter - 
 SaveFileDialog
             *     FilterIndex - 
 SaveFileDialog
             *     ShowDialog() - 
显示打开文件对话框。用户在对话框中单击打开则返回 true;单击取消关闭对话框则返回 false
             *     File - 
返回用户所选择文件的的 FileInfo 对象
             *     Multiselect - 
选择文件时可否多选
             *     Files - 
返回用户所选择文件的的 FileInfo 对象集合
             */


            OpenFileDialog dialog = 
new OpenFileDialog();
            dialog.Filter = 
"Text Files|*.txt";
            
            
if (dialog.ShowDialog() == true)
            
@H_834_404@ {
                
using (FileStream fs = dialog.File.OpenRead())
                
@H_834_404@ {
                    
byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, buffer.Length);

                    txtInfo.Text = Encoding.UTF8.GetString(buffer, buffer.Length);                }            }        }    }}

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

相关推荐