1.WPF之前功能和解决方法
控件表单 Windows Forms
2D 图形API
3D DirectX API
流视频 Windows Media Player API
流文档 PDF
简单UI元素
交互的二维,三维图像
动画
多媒体
2.Application
Current
MainWindow
Properties
StartupUri
Windows
//
class MyApp : Application
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Startup += (s, e)=>{};
app.Exit += (s, e)=>{};
}
}
//
static void MinimizeAllWindows()
{
foreach(Window wnd in Application.Current.Windows)
{
wnd.WindowState = WindowState.Minimized;
}
}
3.System.Windows.Controls.Control
Background, Foreground, BorderBrush, BorderThickness, Padding, HorizontalContentAlignment, VerticalContentAlignment
FontFamily, FontSize, FontStretch, FontWeight
IsTabStop, TabIndex
MouseDoubleClick, PreviewMouseDoubleClick
Template
4.System.Windows.FrameworkElement
ActualHeight, ActualWidth, MaxHeight, MaxWidth, MinHeight, MinWidth, Height, Width
ContextMenu
Cursor
HorizontalAlignment, VerticalAlignment
Name
Resources
ToolTip
5.System.Windows.UIElement
Focusable, IsFocused
IsEnabled
IsMouseDirectlyOver和IsMouseOver
IsVisible, Visibility
RederTransform
6.XAML属性语法
<Button Height = "43">
Button>
等价与
<Button>
<Button.Height>
43
Button.Height>
Button>
7.窗口或任何ContentControl的子类的Content属性只能设置一个对象。
如果窗口需要包含多个元素,那么这些元素须被安排在多个面板中。
面板可以容纳所有表示窗口的UI元素,而面板本身将作为唯一的对象,赋给窗口的Content属性。
8.
// XAML
"WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowStartupLocation="CenterScreen">
"ApplicationCommands.Open" Executed="OpenCmdExecuted" CanExecute="OpenCmdCanExecute"/>
"ApplicationCommands.Save" Executed="SaveCmdExecuted" CanExecute="SaveCmdCanExecute"/>
"Top">
"Bottom" Background="Beige">
"statBarText" Text="Ready"/>
"Left" Background="AliceBlue">
"0" Width="5" Background="Gray"/>
"0" VerticalAlignment="Stretch">
"expanderSpelling" Header="Try these!"
Margin="10, 10, 10, 10">
"1"
SpellCheck.IsEnabled="True"
AcceptsReturn="True"
Name="txtData"
FontSize="14"
BorderBrush="Blue"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
// 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Markup;
using System.Windows.Forms;
namespace WpfApplication2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SetF1CommandBinding();
}
private void SetF1CommandBinding()
{
CommandBinding helpBinding = new CommandBinding(ApplicationCommands.Help);
helpBinding.CanExecute += CanHelpExecute;
helpBinding.Executed += HelpExecuted;
CommandBindings.Add(helpBinding);
}
private void CanHelpExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Look, it is not that difficult. Just type something!", "Help!");
}
private void Window_Closed(object sender, EventArgs e)
{}
//private void MouseEnterExitArea(object sender, RoutedEventArgs e)
//{}
// private void MouseLeaveArea(object sender, RoutedEventArgs e)
//{}
// private void MouseEnterToolsHintArea(object sender, RoutedEventArgs e)
//{}
private void ToolsSpellingHints_Click(object sender, RoutedEventArgs e)
{
string spellingHints = string.Empty;
SpellingError error = txtData.GetSpellingError(txtData.CaretIndex);
if (error != null)
{
foreach (string s in error.Suggestions)
{
spellingHints += string.Format("{0}\n", s);
}
lblSpellingHints.Content = spellingHints;
expanderSpelling.IsExpanded = true;
}
}
private void FileExit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void MouseEnterToolsHintsArea(object sender, RoutedEventArgs e)
{
}
protected void MouseEnterExitArea(object sender, RoutedEventArgs args)
{
statBarText.Text = "Exit the Application";
}
protected void MouseEnterToolsHintArea(object sender, RoutedEventArgs args)
{
statBarText.Text = "Show Spelling Suggestions";
}
protected void MouseLeaveArea(object sender, RoutedEventArgs args)
{
statBarText.Text = "Ready";
}
private void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void SaveCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void OpenCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "Text Files | *.txt";
DialogResult _nRet = openDlg.ShowDialog();
if (_nRet == System.Windows.Forms.DialogResult.OK)
{
string dataFromFile = File.ReadAllText(openDlg.FileName);
txtData.Text = dataFromFile;
}
}
private void SaveCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "Text Files | *.txt";
DialogResult _nRet = saveDlg.ShowDialog();
if (_nRet == System.Windows.Forms.DialogResult.OK)
{
File.WriteAllText(saveDlg.FileName, txtData.Text);
}
}
}
}
9.Document API
System.Windows.Documents
List
Paragraph
Section
Table
LineBreak
Figure
Floater
Span
块元素:
System.Windows.Documents.Block
内联元素:
System.Windows.Document.Inline
文档布局管理器:
FlowDocument
FixDocument
FlowDocumentReader
FlowDocumentScrollViewer
RichTextBox
FlowDocumentPageViewer
10.
Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:a ="clr-namespace:System.Windows.Annotations;assembly=PresentationFramework"
Title="MainWindow" Height="350" Width="525"
WindowStartupLocation="CenterScreen">
"110,75,7,4.5">
"myTabSystem" HorizontalAlignment="Left" Height="280"
Margin="10,0,-96,0" VerticalAlignment="Top" Width="489">
"Ink API">
"0,0,98,36" Orientation="Vertical">
"myInkCanvas" Height="210"/>
"tabDocuments" Header="Documents" VerticalAlignment="Bottom" Height="20">
"Green" Command="a:AnnotationService.CreateTextStickyNoteCommand" Content="Add Sticky Note"/>
"Green" Command="a:AnnotationService.DeleteStickyNotesCommand" Content="Delete Sticky Notes"/>
"Green" Command="a:AnnotationService.CreateHighlightCommand" Content="Highlight Text"/>
"btnSaveDoc" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="75" Content="Save Doc"/>
"btnLoadDoc" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="75" Content="Load Doc"/>
"myDocumentReader" Height="269.4">
"Yellow" Background="Black">
"20">
Here are some fun facts about the WPF Documents API!
"listOfFunFacts"/>
"paraBodyText"/>
"tabDataBinding" Header="Data Binding">
"250" DataContext="{Binding ElementName=mySB}">
"tabDataGrid" Header="DataGrid">
"gridInventory" Height="288"/>
"inkToolBar" Height="60">
"0,2,0,3" Width="300">
"RadioButtonClicked" x:Name="inkRadio" Content="Ink Mode!" GroupName="InkMode" Margin="0,0,200,0"/>
"RadioButtonClicked" Content="Erase Mode!" GroupName="InkMode" Margin="100,0"/>
"RadioButtonClicked" Content="Select Mode!" GroupName="InkMode" Margin="200,0,0,0"/>
"comboColors" Width="100">
"Red"/>
"Green"/>
"Blue"/>
"btnSave" Click="SaveData" Content="Button"/>
"btnLoad" Click="LoadData" Content="Button"/>
"btnClear" Click="Clear" Content="Button"/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Markup;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Annotations;
using System.Windows.Annotations.Storage;
namespace WpfApplication2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.myInkCanvas.EditingMode = InkCanvasEditingMode.Ink;
this.inkRadio.IsChecked = true;
this.comboColors.SelectedIndex = 0;
PopolateDocument();
EnableAnnotations();
btnSaveDoc.Click += (o, s) =>
{
using(FileStream fStream = File.Open("documentData.xaml", FileMode.Create))
{
XamlWriter.Save(this.myDocumentReader.Document, fStream);
}
};
btnLoadDoc.Click += (o, s) =>
{
using (FileStream fStream = File.Open("documentData.xaml", FileMode.Open))
{
try
{
FlowDocument doc = XamlReader.Load(fStream) as FlowDocument;
this.myDocumentReader.Document = doc;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Error Loading Doc!");
}
}
};
SetBindings();
}
private void SetBindings()
{
System.Windows.Data.Binding b = new System.Windows.Data.Binding();
b.Converter = new MyDoubleConverter();
b.Source = this.mySB;
b.Path = new PropertyPath("Value");
this.labelSBThumb.SetBinding(System.Windows.Controls.Label.ContentProperty, b);
}
private void EnableAnnotations()
{
AnnotationService anoService = new AnnotationService(myDocumentReader);
MemoryStream anoStream = new MemoryStream();
AnnotationStore store = new XmlStreamStore(anoStream);
anoService.Enable(store);
}
private void RadioButtonClicked(object sender, System.Windows.RoutedEventArgs e)
{
switch((sender as System.Windows.Controls.RadioButton).Content.ToString())
{
case "Ink Mode!":
{
this.myInkCanvas.EditingMode = InkCanvasEditingMode.Ink;
}
break;
case "Erase Mode!":
{
this.myInkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
}
break;
case "Select Mode!":
{
this.myInkCanvas.EditingMode = InkCanvasEditingMode.Select;
}
break;
}
}
private void ColorChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string colorToUse = (this.comboColors.SelectedItem as StackPanel).Tag.ToString();
}
private void SaveData(object sender, System.Windows.RoutedEventArgs e)
{
using(FileStream fs = new FileStream("StrokeData.bin", FileMode.Create))
{
this.myInkCanvas.Strokes.Save(fs);
fs.Close();
}
}
private void LoadData(object sender, System.Windows.RoutedEventArgs e)
{
using(FileStream fs = new FileStream("StrokeData.bin", FileMode.Open, FileAccess.Read))
{
StrokeCollection strokes = new StrokeCollection(fs);
this.myInkCanvas.Strokes = strokes;
}
}
private void Clear(object sender, System.Windows.RoutedEventArgs e)
{
this.myInkCanvas.Strokes.Clear();
}
private void PopolateDocument()
{
this.listOfFunFacts.FontSize = 14;
this.listOfFunFacts.MarkerStyle = TextMarkerStyle.Circle;
this.listOfFunFacts.ListItems.Add(new ListItem(new Paragraph(new Run("Fixed documents are for WYSIWYG print ready docs!"))));
this.listOfFunFacts.ListItems.Add(new ListItem(new Paragraph(new Run("The API supports tables and embedded figures!"))));
this.listOfFunFacts.ListItems.Add(new ListItem(new Paragraph(new Run("Flow documents are read only!"))));
this.listOfFunFacts.ListItems.Add(new ListItem(new Paragraph(new Run("BlockUIContainer allows you to embed WPF controls in the document!"))));
Run prefix = new Run("This paragraph was generated");
Bold b = new Bold();
Run infix = new Run("dynamically");
infix.Foreground = Brushes.Red;
infix.FontSize = 30;
b.Inlines.Add(infix);
Run suffix = new Run("at runtime!");
this.paraBodyText.Inlines.Add(prefix);
this.paraBodyText.Inlines.Add(infix);
this.paraBodyText.Inlines.Add(suffix);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace WpfApplication2
{
class MyDoubleConverter : IValueConverter
{
// 源--> 目标
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double v = (double)value;
return (int)v;
}
// 目标-->源
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
}