去哪里购买网站空间,wordpress登陆,佛山营销网站建设制作,宜昌市住房和城乡建设局网站WPF的基础控件详解 在WPF学习中 基本控件是最简单也是最基础的东西。也是很初学者容易忽略的 本此笔记教程主要针对WPF中基础控件使用和应用进行手把手教学#xff0c;如果学习了此笔记对你有帮助记得一键三连哦~~~~ TextBlock
基本用法
长字串处理
LineBreak标籤在指定的地…WPF的基础控件详解 在WPF学习中 基本控件是最简单也是最基础的东西。也是很初学者容易忽略的 本此笔记教程主要针对WPF中基础控件使用和应用进行手把手教学如果学习了此笔记对你有帮助记得一键三连哦~~~~ TextBlock
基本用法
长字串处理
LineBreak标籤在指定的地方手动换行。TextTrimming属性并设为**CharacterEllipsis**让TextBlock在没有足够空间的时候显示…。在没有足够空间显示这麽多文字时这是一种常见的方法。这也很适合用在当空间不足而且你不想使用多行的时候。你也可以使用**CharacterEllipsis**的另一种**WordEllipsis**当空间不足的时候以最后一个单字为单位显示就不会有单字只显示一部分的问题发生。TextWrapping 属性并设为**Wrap**让TextBlock在没有足够空间显示时自动换行。
TextBlock内联格式
粗体(Bold), 斜体(Italic), 下划线(Underline)
超连接 (Hyperlink)
Span块的使用
C# 或逻辑代码格式化文本
TextBlock tb new TextBlock();
tb.TextWrapping TextWrapping.Wrap;
tb.Margin new Thickness(10);
tb.Inlines.Add(An example on );
tb.Inlines.Add(new Run(the TextBlock control ) { FontWeight FontWeights.Bold });
tb.Inlines.Add(using );
tb.Inlines.Add(new Run(inline ) { FontStyle FontStyles.Italic });
tb.Inlines.Add(new Run(text formatting ) { Foreground Brushes.Blue });
tb.Inlines.Add(from );
tb.Inlines.Add(new Run(Code-Behind) { TextDecorations TextDecorations.Underline });
tb.Inlines.Add(.);
this.Content tb;
Label
Label和访问键(助记符)
StackPanel Margin10Label Content_Name: Target{Binding ElementNametxtName} /TextBox NametxtName /Label Content_Mail: Target{Binding ElementNametxtMail} /TextBox NametxtMail /
/StackPanel
使用控件作为Label的内容
Label Target{Binding ElementNametxtName}StackPanel OrientationHorizontalImage Sourcehttp://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_green.png /AccessText Text_Name: //StackPanel
/Label
TextBox NametxtName /
Label Target{Binding ElementNametxtMail}StackPanel OrientationHorizontalImage Sourcehttp://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_blue.png /AccessText Text_Mail: //StackPanel
/Label
TextBox NametxtMail /
Label控件和TextBlock控件的对比
那为什么要使用Label呢 好吧Label和TextBlock之间有一些重要的区别。 TextBlock仅允许您呈现文本字串而Label还允许您做下列的事情
设定边界(border)渲染其他控件例如一张图片通过ContentTemplate属性使用模板化的内容使用访问键聚焦到相关的控件上
最后一个点是使用Label取代TextBlock控件的其中一个主要原因.当你只是需要渲染简单的文本内容时,你应该使用TextBlock控件,因为它更轻量并且在大多数场景下性能比Label好.
TextBox
单行TextBox 多行文本框 有拼写检查的TextBox
SpellCheck类中名为IsEnabled的附加属性该属性仅支持对父控件进行拼写检查以及Language属性该属性指示拼写检查器使用的语言。
使用TextBox的选择属性
DockPanel Margin10TextBox SelectionChangedTextBox_SelectionChanged DockPanel.DockTop /TextBox NametxtStatus AcceptsReturnTrue TextWrappingWrap IsReadOnlyTrue /
/DockPanel
选择事件
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{TextBox textBox sender as TextBox;txtStatus.Text Selection starts at character # textBox.SelectionStart Environment.NewLine;txtStatus.Text Selection is textBox.SelectionLength character(s) long Environment.NewLine;txtStatus.Text Selected text: textBox.SelectedText ;
} 我们使用三个相关的属性来实现 SelectionStart它给出了当前光标位置或是否有选择它从什么位置开始。 SelectionLength它给出了当前选择的长度如果有的话。 否则它将返回0。 SelectedText如果有选择它会给我们当前选择的字符串。 否则返回一个空字符串。 Button
简单的按钮
格式化内容
具有高级内容的按钮
ButtonStackPanel OrientationHorizontalTextBlockFormatted /TextBlockTextBlock ForegroundBlue FontWeightBold Margin2,0Button/TextBlockTextBlock ForegroundGray FontStyleItalic[Various]/TextBlock/StackPanel
/Button
带图片的按钮ImageButton
Button Padding5 StackPanel OrientationHorizontal Image Source/WpfTutorialSamples;component/Images/help.png / TextBlock Margin5,0Help/TextBlock /StackPanel
/Button
按钮填充
Button Padding5,2Hello, World!/Button基于通用样式编写
Window.ResourcesStyle TargetType{x:Type Button}Setter PropertyPadding Value5,2//Style
/Window.Resources
CheckBox
基本内容
自定义内容
IsThreeState 属性 IsThreeState 的一般用法是创建一个“全部启用”的 CheckBox让它控制一系列的子 CheckBox并显示它们作为一个整体的状态。我们下面的例子展示了如何创建几个可开关的功能并在窗口最上面有一个“全部启用”的 CheckBox StackPanel Margin10Label FontWeightBoldApplication Options/LabelStackPanel Margin10,5CheckBox IsThreeStateTrue NamecbAllFeatures CheckedcbAllFeatures_CheckedChanged UncheckedcbAllFeatures_CheckedChangedEnable all/CheckBoxStackPanel Margin20,5CheckBox NamecbFeatureAbc CheckedcbFeature_CheckedChanged UncheckedcbFeature_CheckedChangedEnable feature ABC/CheckBoxCheckBox NamecbFeatureXyz IsCheckedTrue CheckedcbFeature_CheckedChanged UncheckedcbFeature_CheckedChangedEnable feature XYZ/CheckBoxCheckBox NamecbFeatureWww CheckedcbFeature_CheckedChanged UncheckedcbFeature_CheckedChangedEnable feature WWW/CheckBox/StackPanel/StackPanel
/StackPanel
private void cbAllFeatures_CheckedChanged(object sender, RoutedEventArgs e)
{bool newVal (cbAllFeatures.IsChecked true);cbFeatureAbc.IsChecked newVal;cbFeatureXyz.IsChecked newVal;cbFeatureWww.IsChecked newVal;
}
private void cbFeature_CheckedChanged(object sender, RoutedEventArgs e)
{cbAllFeatures.IsChecked null;if((cbFeatureAbc.IsChecked true) (cbFeatureXyz.IsChecked true) (cbFeatureWww.IsChecked true))cbAllFeatures.IsChecked true;if((cbFeatureAbc.IsChecked false) (cbFeatureXyz.IsChecked false) (cbFeatureWww.IsChecked false))cbAllFeatures.IsChecked false;
}
RadioButton
基本用法
RadioButton 组 GroupNameready
自定义内容
StackPanel Margin10Label FontWeightBoldAre you ready?/LabelRadioButtonWrapPanelImage Source/WpfTutorialSamples;component/Images/accept.png Width16 Height16 Margin0,0,5,0 /TextBlock TextYes ForegroundGreen //WrapPanel/RadioButtonRadioButton Margin0,5WrapPanelImage Source/WpfTutorialSamples;component/Images/cancel.png Width16 Height16 Margin0,0,5,0 /TextBlock TextNo ForegroundRed //WrapPanel/RadioButtonRadioButton IsCheckedTrueWrapPanelImage Source/WpfTutorialSamples;component/Images/question.png Width16 Height16 Margin0,0,5,0 /TextBlock TextMaybe ForegroundGray //WrapPanel/RadioButton
/StackPanel
PasswordBox
基本用法PasswordChar
Image
Source属性
动态加载图片后置代码
StackPanelWrapPanel Margin10 HorizontalAlignmentCenterButton NamebtnLoadFromFile Margin0,0,20,0 ClickBtnLoadFromFile_ClickLoad from File.../ButtonButton NamebtnLoadFromResource ClickBtnLoadFromResource_ClickLoad from Resource/Button/WrapPanelImage NameimgDynamic Margin10 /
/StackPanel
private void BtnLoadFromFile_Click(object sender, RoutedEventArgs e)
{OpenFileDialog openFileDialog new OpenFileDialog();if(openFileDialog.ShowDialog() true){Uri fileUri new Uri(openFileDialog.FileName);imgDynamic.Source new BitmapImage(fileUri);}
}private void BtnLoadFromResource_Click(object sender, RoutedEventArgs e)
{Uri resourceUri new Uri(/Images/white_bengal_tiger.jpg, UriKind.Relative);imgDynamic.Source new BitmapImage(resourceUri);
}
Stretch属性
Uniform: 这是默认模式。 图片将自动缩放以便它适合图片区域。 将保留图片的宽高比。UniformToFill: 图片将被缩放以便完全填充图片区域。 将保留图片的宽高比。Fill: 图片将缩放以适合图片控件的区域。 可能无法保留宽高比因为图片的高度和宽度是独立缩放的。None: 如果图片小于图片控件则不执行任何操作。 如果它比图片控件大则会裁剪图片以适合图片控件这意味着只有部分图片可见。
GridGrid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* /ColumnDefinition Width* //Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition HeightAuto /RowDefinition Height* //Grid.RowDefinitionsLabel Grid.Column0 HorizontalAlignmentCenter FontWeightBoldUniform/LabelLabel Grid.Column1 HorizontalAlignmentCenter FontWeightBoldUniformToFill/LabelLabel Grid.Column2 HorizontalAlignmentCenter FontWeightBoldFill/LabelLabel Grid.Column3 HorizontalAlignmentCenter FontWeightBoldNone/LabelImage Source/Images/white_bengal_tiger.jpg StretchUniform Grid.Column0 Grid.Row1 Margin5 /Image Source/Images/white_bengal_tiger.jpg StretchUniformToFill Grid.Column1 Grid.Row1 Margin5 /Image Source/Images/white_bengal_tiger.jpg StretchFill Grid.Column2 Grid.Row1 Margin5 /Image Source/Images/white_bengal_tiger.jpg StretchNone Grid.Column3 Grid.Row1 Margin5 /
/Grid
ToolTip
基础用法
Grid VerticalAlignmentCenter HorizontalAlignmentCenterButton ToolTipClick here and something will happen!Click here!/Button
/Grid
应用
DockPanelToolBar DockPanel.DockTopButton ToolTipCreate a new fileButton.ContentImage Source/WpfTutorialSamples;component/Images/page_white.png Width16 Height16 //Button.Content/ButtonButtonButton.ContentImage Source/WpfTutorialSamples;component/Images/folder.png Width16 Height16 //Button.ContentButton.ToolTipStackPanelTextBlock FontWeightBold FontSize14 Margin0,0,0,5Open file/TextBlockTextBlockSearch your computer or local networkLineBreak /for a file and open it for editing./TextBlockBorder BorderBrushSilver BorderThickness0,1,0,0 Margin0,8 /WrapPanelImage Source/WpfTutorialSamples;component/Images/help.png Margin0,0,5,0 /TextBlock FontStyleItalicPress F1 for more help/TextBlock/WrapPanel/StackPanel/Button.ToolTip/Button/ToolBarTextBoxEditor area.../TextBox
/DockPanel
高级选项ShowDuration ShowDuration 属性扩展工具提示的时间我们将其设置为5.000毫秒或5秒 WPF文本渲染
控制文本渲染TextFormattingMode
StackPanel Margin10Label TextOptions.TextFormattingModeIdeal FontSize9TextFormattingMode.Ideal, small text/LabelLabel TextOptions.TextFormattingModeDisplay FontSize9TextFormattingMode.Display, small text/LabelLabel TextOptions.TextFormattingModeIdeal FontSize20TextFormattingMode.Ideal, large text/LabelLabel TextOptions.TextFormattingModeDisplay FontSize20TextFormattingMode.Display, large text/Label
/StackPanel
TextRenderingMode
StackPanel Margin10 TextOptions.TextFormattingModeDisplayLabel TextOptions.TextRenderingModeAuto FontSize9TextRenderingMode.Auto, small text/LabelLabel TextOptions.TextRenderingModeAliased FontSize9TextRenderingMode.Aliased, small text/LabelLabel TextOptions.TextRenderingModeClearType FontSize9TextRenderingMode.ClearType, small text/LabelLabel TextOptions.TextRenderingModeGrayscale FontSize9TextRenderingMode.Grayscale, small text/LabelLabel TextOptions.TextRenderingModeAuto FontSize18TextRenderingMode.Auto, large text/LabelLabel TextOptions.TextRenderingModeAliased FontSize18TextRenderingMode.Aliased, large text/LabelLabel TextOptions.TextRenderingModeClearType FontSize18TextRenderingMode.ClearType, large text/LabelLabel TextOptions.TextRenderingModeGrayscale FontSize18TextRenderingMode.Grayscale, large text/Label
/StackPanel
Tab顺序
TabIndex和**IsTabStop**。 TabIndex用于定义顺序而IsTabStop将强制WPF在窗口按Tab时跳过这个控件。
Grid Margin20Grid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width20 /ColumnDefinition Width* //Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition Height* /RowDefinition HeightAuto //Grid.RowDefinitionsStackPanelLabelFirst name:/LabelTextBox /LabelStreet name:/LabelTextBox /LabelCity:/LabelTextBox IsReadOnlyTrue IsTabStopFalse BackgroundLightYellow //StackPanelStackPanel Grid.Column2LabelLast name:/LabelTextBox /LabelZip Code:/LabelTextBox //StackPanelButton Grid.Row1 HorizontalAlignmentRight Width80Add/ButtonButton Grid.Row1 Grid.Column2 HorizontalAlignmentLeft Width80Cancel/Button
/Grid
Border
基础用法
圆角边界
Grid Margin10Border BackgroundGhostWhite BorderBrushSilver BorderThickness1 CornerRadius8,8,3,3StackPanel Margin10ButtonButton 1/ButtonButton Margin0,10Button 2/ButtonButtonButton 3/Button/StackPanel/Border
/Grid
边界颜色/宽度
Grid Margin10Border BackgroundGhostWhite BorderBrushDodgerBlue BorderThickness1,3,1,5StackPanel Margin10ButtonButton 1/ButtonButton Margin0,10Button 2/ButtonButtonButton 3/Button/StackPanel/Border
/Grid
边界背景
Grid Margin10Border BorderBrushNavy BorderThickness1,3,1,5Border.BackgroundLinearGradientBrush StartPoint0.5,0 EndPoint0.5,1GradientStop ColorLightCyan Offset0.0 /GradientStop ColorLightBlue Offset0.5 /GradientStop ColorDarkTurquoise Offset1.0 //LinearGradientBrush/Border.BackgroundStackPanel Margin10ButtonButton 1/ButtonButton Margin0,10Button 2/ButtonButtonButton 3/Button/StackPanel/Border
/Grid
Slider
基本用法
刻度
StackPanel VerticalAlignmentCenter Margin10Slider Maximum100 TickPlacementBottomRight TickFrequency5 /
/StackPanel
捕获标记
StackPanel VerticalAlignmentCenter Margin10Slider Maximum100 TickPlacementBottomRight TickFrequency10 IsSnapToTickEnabledTrue /
/StackPanel
Slider值
DockPanel VerticalAlignmentCenter Margin10TextBox Text{Binding ElementNameslValue, PathValue, UpdateSourceTriggerPropertyChanged} DockPanel.DockRight TextAlignmentRight Width40 /Slider Maximum255 TickPlacementBottomRight TickFrequency5 IsSnapToTickEnabledTrue NameslValue /
/DockPanel
响应变化的值
StackPanel Margin10 VerticalAlignmentCenterDockPanel VerticalAlignmentCenter Margin10Label DockPanel.DockLeft FontWeightBoldR:/LabelTextBox Text{Binding ElementNameslColorR, PathValue, UpdateSourceTriggerPropertyChanged} DockPanel.DockRight TextAlignmentRight Width40 /Slider Maximum255 TickPlacementBottomRight TickFrequency5 IsSnapToTickEnabledTrue NameslColorR ValueChangedColorSlider_ValueChanged //DockPanelDockPanel VerticalAlignmentCenter Margin10Label DockPanel.DockLeft FontWeightBoldG:/LabelTextBox Text{Binding ElementNameslColorG, PathValue, UpdateSourceTriggerPropertyChanged} DockPanel.DockRight TextAlignmentRight Width40 /Slider Maximum255 TickPlacementBottomRight TickFrequency5 IsSnapToTickEnabledTrue NameslColorG ValueChangedColorSlider_ValueChanged //DockPanelDockPanel VerticalAlignmentCenter Margin10Label DockPanel.DockLeft FontWeightBoldB:/LabelTextBox Text{Binding ElementNameslColorB, PathValue, UpdateSourceTriggerPropertyChanged} DockPanel.DockRight TextAlignmentRight Width40 /Slider Maximum255 TickPlacementBottomRight TickFrequency5 IsSnapToTickEnabledTrue NameslColorB ValueChangedColorSlider_ValueChanged //DockPanel
/StackPanel
private void ColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgsdouble e)
{Color color Color.FromRgb((byte)slColorR.Value, (byte)slColorG.Value, (byte)slColorB.Value);this.Background new SolidColorBrush(color);
}
ProgressBar
基本操作
在执行漫长的任务时显示进度
Grid Margin20ProgressBar Minimum0 Maximum100 NamepbStatus /
/Grid
private void Window_ContentRendered(object sender, EventArgs e)
{for(int i 0; i 100; i){pbStatus.Value;Thread.Sleep(100);}
}
BackgroundWorker
private void Window_ContentRendered(object sender, EventArgs e)
{BackgroundWorker worker new BackgroundWorker();worker.WorkerReportsProgress true;worker.DoWork worker_DoWork;worker.ProgressChanged worker_ProgressChanged;worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{for(int i 0; i 100; i){(sender as BackgroundWorker).ReportProgress(i);Thread.Sleep(100);}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{pbStatus.Value e.ProgressPercentage;
}
不确定
Grid Margin20ProgressBar Minimum0 Maximum100 NamepbStatus IsIndeterminateTrue /
/Grid
ProgressBar显示文本
Grid Margin20ProgressBar Minimum0 Maximum100 Value75 NamepbStatus /TextBlock Text{Binding ElementNamepbStatus, PathValue, StringFormat{}{0:0}%} HorizontalAlignmentCenter VerticalAlignmentCenter /
/Grid
WebBrowser
Window.CommandBindingsCommandBinding CommandNavigationCommands.BrowseBack CanExecuteBrowseBack_CanExecute ExecutedBrowseBack_Executed /CommandBinding CommandNavigationCommands.BrowseForward CanExecuteBrowseForward_CanExecute ExecutedBrowseForward_Executed /CommandBinding CommandNavigationCommands.GoToPage CanExecuteGoToPage_CanExecute ExecutedGoToPage_Executed /
/Window.CommandBindings
DockPanelToolBar DockPanel.DockTopButton CommandNavigationCommands.BrowseBackImage Source/WpfTutorialSamples;component/Images/arrow_left.png Width16 Height16 //ButtonButton CommandNavigationCommands.BrowseForwardImage Source/WpfTutorialSamples;component/Images/arrow_right.png Width16 Height16 //ButtonSeparator /TextBox NametxtUrl Width300 KeyUptxtUrl_KeyUp /Button CommandNavigationCommands.GoToPageImage Source/WpfTutorialSamples;component/Images/world_go.png Width16 Height16 //Button/ToolBarWebBrowser NamewbSample NavigatingwbSample_Navigating/WebBrowser
/DockPanel
public partial class WebBrowserControlSample : Window
{public WebBrowserControlSample(){InitializeComponent();wbSample.Navigate(http://www.wpf-tutorial.com);}private void txtUrl_KeyUp(object sender, KeyEventArgs e){if(e.Key Key.Enter)wbSample.Navigate(txtUrl.Text);}private void wbSample_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e){txtUrl.Text e.Uri.OriginalString;}private void BrowseBack_CanExecute(object sender, CanExecuteRoutedEventArgs e){e.CanExecute ((wbSample ! null) (wbSample.CanGoBack));}private void BrowseBack_Executed(object sender, ExecutedRoutedEventArgs e){wbSample.GoBack();}private void BrowseForward_CanExecute(object sender, CanExecuteRoutedEventArgs e){e.CanExecute ((wbSample ! null) (wbSample.CanGoForward));}private void BrowseForward_Executed(object sender, ExecutedRoutedEventArgs e){wbSample.GoForward();}private void GoToPage_CanExecute(object sender, CanExecuteRoutedEventArgs e){e.CanExecute true;}private void GoToPage_Executed(object sender, ExecutedRoutedEventArgs e){wbSample.Navigate(txtUrl.Text);}
}
WindowsFromsHost
使用WinForms WebBrowser控件
GridWindowsFormsHost NamewfhSampleWindowsFormsHost.Childwf:WebBrowser DocumentTitleChangedwbWinForms_DocumentTitleChanged //WindowsFormsHost.Child/WindowsFormsHost
/Grid
public partial class WindowsFormsHostSample : Window
{public WindowsFormsHostSample(){InitializeComponent();(wfhSample.Child as System.Windows.Forms.WebBrowser).Navigate(http://www.wpf-tutorial.com);}private void wbWinForms_DocumentTitleChanged(object sender, EventArgs e){this.Title (sender as System.Windows.Forms.WebBrowser).DocumentTitle;}
}
组合框
基本用法
GridGroupBox HeaderGroupBox Sample Margin10 Padding10StackPanelTextBlockFirst name:/TextBlockTextBox /TextBlockLast name:/TextBlockTextBox /Button Margin0,20Add User/Button/StackPanel/GroupBox
/Grid
自定义标题组合框
GridGroupBox Margin10 Padding10GroupBox.HeaderStackPanel OrientationHorizontalImage Source/WpfTutorialSamples;component/Images/group.png Margin3,0 /TextBlock FontWeightBoldGroupBox Sample/TextBlock/StackPanel/GroupBox.HeaderStackPanelTextBlockFirst name:/TextBlockTextBox /TextBlockLast name:/TextBlockTextBox /Button Margin0,20Add User/Button/StackPanel/GroupBox
/Grid 注意我这里用**GroupBox.Header**标记简单替换了Header属性随后添加了一个StackPanel 以便包含一个 Image 和一个 TextBlock —— 这样一来你就能对 Header 完全自定义了 日历控件
基本用法
GridCalendar /
/Grid
日历大小
ViewboxCalendar /
/ViewboxViewbox StretchFill StretchDirectionUpOnlyCalendar /
/Viewbox
DisplayDate设置初始化视图
ViewboxCalendar DisplayDate01.01.2014 /
/Viewbox
Calendar 选中模式
ViewboxCalendar SelectionModeSingleRange /Calendar SelectionModeMultipleRange /
/Viewbox
选中日期
StackPanel Margin10Calendar NamecldSample SelectionModeMultipleRange SelectedDate10.10.2013 /LabelSelected date:/LabelTextBox Text{Binding ElementNamecldSample, PathSelectedDate, StringFormatd, UpdateSourceTriggerPropertyChanged} /
/StackPanel
public CalendarSelectionSample()
{InitializeComponent();cldSample.SelectedDate DateTime.Now.AddDays(1);
}
多选日期
StackPanel Margin10Calendar NamecldSample SelectionModeMultipleRange /LabelSelected dates:/LabelListBox ItemsSource{Binding ElementNamecldSample, PathSelectedDates} MinHeight150 /
/StackPanel
删除日期
ViewboxCalendar NamecldSample SelectionModeMultipleRangeCalendar.BlackoutDatesCalendarDateRange Start10.13.2013 End10.19.2013 /CalendarDateRange Start10.27.2013 End10.31.2013 //Calendar.BlackoutDates/Calendar
/Viewbox
public CalendarBlockedoutDatesSample()
{InitializeComponent();cldSample.BlackoutDates.AddDatesInPast();cldSample.BlackoutDates.Add(new CalendarDateRange(DateTime.Today, DateTime.Today.AddDays(1)));
}
显示模式-显示月份或者年份 DisplayMode属性能够将Calendar 控件从一个你能选中一个日期的位置更改为到你能够选中一个月甚至一年的位置这是能通过DisplayMode 做到该属性默认为月份我们已经在之前的例子中都使用到了 ViewboxCalendar DisplayModeYear /
/Viewbox
DisplayMode [Year, Decade]
日历选择器
基本用法
添加一个DatePicker 控件
StackPanel Margin20LabelName:/LabelTextBox /LabelBirthday:/LabelDatePicker/DatePickerLabelGender:/LabelComboBoxComboBoxItemFemale/ComboBoxItemComboBoxItemMale/ComboBoxItem/ComboBoxButton Margin20Signup/Button
/StackPanel
显示日期和选择日期
DatePicker SelectedDate2000-12-31/DatePicker
DatePicker Namedp1 DisplayDate2019-01-01 /
选择日期格式
DatePicker SelectedDate2000-12-31 SelectedDateFormatLong/DatePicker排除日期
DatePicker Namedp1DatePicker.BlackoutDatesCalendarDateRange Start2019-04-01 End2019-04-07 /CalendarDateRange Start2019-04-22 End2019-04-28 //DatePicker.BlackoutDates
/DatePicker
扩展控件
它的代码当然非常简单
Expander TextBlock TextWrappingWrap FontSize18 Here we can have text which can be hidden/shown using the built-in functionality of the Expander control. /TextBlock
/Expander
Expander 默认是不会扩展开来的所以就想第一张截图所看到那样。用户可以通过点击它扩展或者你能让它通过**IsExpanded** 属性初始时扩展
Expander IsExpandedTrue你当然也能在运行时读到这个属性如果你需要知道关于Expander 的当前状态。
高级内容
Expander Margin10StackPanel Margin10DockPanelImage Source/WpfTutorialSamples;component/Images/question32.png Width32 Height32 DockPanel.DockRight Margin10/ImageTextBlock TextWrappingWrap FontSize18Did you know that WPF is really awesome? Just enter your e-mail address below and well send you updates:/TextBlock/DockPanelTextBox Margin10johndoe.org/TextBox/StackPanel
/Expander
扩展方向
Expander Margin10 ExpandDirectionRightTextBlock TextWrappingWrap FontSize18Here we can have text which can be hidden/shown using the built-in functionality of the Expander control./TextBlock
/Expander
自定义标题
Expander Margin10 HeaderClick to show/hide content...TextBlock TextWrappingWrap FontSize18Here we can have text which can be hidden/shown using the built-in functionality of the Expander control./TextBlock
/Expander Header属性允许你去添加控件进去去创建一个更加定制的外观 Expander Margin10Expander.HeaderDockPanel VerticalAlignmentStretchImage Source/WpfTutorialSamples;component/Images/bullet_green.png Height16 DockPanel.DockLeft /TextBlock FontStyleItalic ForegroundGreenClick to show/hide content.../TextBlock/DockPanel/Expander.HeaderTextBlock TextWrappingWrap FontSize18Here we can have text which can be hidden/shown using the built-in functionality of the Expander control./TextBlock
/Expander
ItemsControl
一个简单的ItemsControl范例
Grid Margin10ItemsControlsystem:StringItemsControl Item #1/system:Stringsystem:StringItemsControl Item #2/system:Stringsystem:StringItemsControl Item #3/system:Stringsystem:StringItemsControl Item #4/system:Stringsystem:StringItemsControl Item #5/system:String/ItemsControl
/Grid
有数据绑定的ItemsControl
Grid Margin10ItemsControl NameicTodoListItemsControl.ItemTemplateDataTemplateGrid Margin0,0,0,5Grid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width100 //Grid.ColumnDefinitionsTextBlock Text{Binding Title} /ProgressBar Grid.Column1 Minimum0 Maximum100 Value{Binding Completion} //Grid/DataTemplate/ItemsControl.ItemTemplate/ItemsControl
/Grid
public partial class ItemsControlDataBindingSample : Window
{public ItemsControlDataBindingSample(){InitializeComponent();ListTodoItem items new ListTodoItem();items.Add(new TodoItem() { Title Complete this WPF tutorial, Completion 45 });items.Add(new TodoItem() { Title Learn C#, Completion 80 });items.Add(new TodoItem() { Title Wash the car, Completion 0 });icTodoList.ItemsSource items;}
}public class TodoItem
{public string Title { get; set; }public int Completion { get; set; }
}
ItemsPanelTemplate属性
Grid Margin10ItemsControlItemsControl.ItemsPanelItemsPanelTemplateWrapPanel //ItemsPanelTemplate/ItemsControl.ItemsPanelItemsControl.ItemTemplateDataTemplateButton Content{Binding} Margin0,0,5,5 //DataTemplate/ItemsControl.ItemTemplatesystem:StringItem #1/system:Stringsystem:StringItem #2/system:Stringsystem:StringItem #3/system:Stringsystem:StringItem #4/system:Stringsystem:StringItem #5/system:String/ItemsControl
/Grid UniformGrid面板我们可以在其中定义多个列然后将我们的项目整齐地显示在同样宽 Grid Margin10ScrollViewer VerticalScrollBarVisibilityAuto HorizontalScrollBarVisibilityAutoItemsControlsystem:StringItemsControl Item #1/system:Stringsystem:StringItemsControl Item #2/system:Stringsystem:StringItemsControl Item #3/system:Stringsystem:StringItemsControl Item #4/system:Stringsystem:StringItemsControl Item #5/system:String/ItemsControl/ScrollViewer
/Grid
ListBox
基本用法
Grid Margin10ListBoxListBoxItemListBox Item #1/ListBoxItemListBoxItemListBox Item #2/ListBoxItemListBoxItemListBox Item #3/ListBoxItem/ListBox
/Grid
ListBox数据绑定
Grid Margin10ListBox NamelbTodoList HorizontalContentAlignmentStretchListBox.ItemTemplateDataTemplateGrid Margin0,2Grid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width100 //Grid.ColumnDefinitionsTextBlock Text{Binding Title} /ProgressBar Grid.Column1 Minimum0 Maximum100 Value{Binding Completion} //Grid/DataTemplate/ListBox.ItemTemplate/ListBox
/Grid
public partial class ListBoxDataBindingSample : Window
{public ListBoxDataBindingSample(){InitializeComponent();ListTodoItem items new ListTodoItem();items.Add(new TodoItem() { Title Complete this WPF tutorial, Completion 45 });items.Add(new TodoItem() { Title Learn C#, Completion 80 });items.Add(new TodoItem() { Title Wash the car, Completion 0 });lbTodoList.ItemsSource items;}
}
public class TodoItem
{public string Title { get; set; }public int Completion { get; set; }
}
ListBox选择
DockPanel Margin10StackPanel DockPanel.DockRight Margin10,0StackPanel.ResourcesStyle TargetTypeButtonSetter PropertyMargin Value0,0,0,5 //Style/StackPanel.ResourcesTextBlock FontWeightBold Margin0,0,0,10ListBox selection/TextBlockButton NamebtnShowSelectedItem ClickbtnShowSelectedItem_ClickShow selected/ButtonButton NamebtnSelectLast ClickbtnSelectLast_ClickSelect last/ButtonButton NamebtnSelectNext ClickbtnSelectNext_ClickSelect next/ButtonButton NamebtnSelectCSharp ClickbtnSelectCSharp_ClickSelect C#/ButtonButton NamebtnSelectAll ClickbtnSelectAll_ClickSelect all/Button/StackPanelListBox NamelbTodoList HorizontalContentAlignmentStretch SelectionModeExtended SelectionChangedlbTodoList_SelectionChangedListBox.ItemTemplateDataTemplateGrid Margin0,2Grid.ColumnDefinitionsColumnDefinition Width* /ColumnDefinition Width100 //Grid.ColumnDefinitionsTextBlock Text{Binding Title} /ProgressBar Grid.Column1 Minimum0 Maximum100 Value{Binding Completion} //Grid/DataTemplate/ListBox.ItemTemplate/ListBox
/DockPanel
public partial class ListBoxSelectionSample : Window
{public ListBoxSelectionSample(){InitializeComponent();ListTodoItem items new ListTodoItem();items.Add(new TodoItem() { Title Complete this WPF tutorial, Completion 45 });items.Add(new TodoItem() { Title Learn C#, Completion 80 });items.Add(new TodoItem() { Title Wash the car, Completion 0 });lbTodoList.ItemsSource items;}private void lbTodoList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e){if(lbTodoList.SelectedItem ! null)this.Title (lbTodoList.SelectedItem as TodoItem).Title;}private void btnShowSelectedItem_Click(object sender, RoutedEventArgs e){foreach(object o in lbTodoList.SelectedItems)MessageBox.Show((o as TodoItem).Title);}private void btnSelectLast_Click(object sender, RoutedEventArgs e){lbTodoList.SelectedIndex lbTodoList.Items.Count - 1;}private void btnSelectNext_Click(object sender, RoutedEventArgs e){int nextIndex 0;if((lbTodoList.SelectedIndex 0) (lbTodoList.SelectedIndex (lbTodoList.Items.Count - 1)))nextIndex lbTodoList.SelectedIndex 1;lbTodoList.SelectedIndex nextIndex;}private void btnSelectCSharp_Click(object sender, RoutedEventArgs e){foreach(object o in lbTodoList.Items){if((o is TodoItem) ((o as TodoItem).Title.Contains(C#))){lbTodoList.SelectedItem o;break;}}}private void btnSelectAll_Click(object sender, RoutedEventArgs e){foreach(object o in lbTodoList.Items)lbTodoList.SelectedItems.Add(o);}
}
public class TodoItem
{public string Title { get; set; }public int Completion { get; set; }
}
ComboBox
基本用法
StackPanel Margin10ComboBoxComboBoxItemComboBox Item #1/ComboBoxItemComboBoxItem IsSelectedTrueComboBox Item #2/ComboBoxItemComboBoxItemComboBox Item #3/ComboBoxItem/ComboBox
/StackPanel
自定义内容
StackPanel Margin10ComboBoxComboBoxItemStackPanel OrientationHorizontalImage Source/WpfTutorialSamples;component/Images/bullet_red.png /TextBlock ForegroundRedRed/TextBlock/StackPanel/ComboBoxItemComboBoxItemStackPanel OrientationHorizontalImage Source/WpfTutorialSamples;component/Images/bullet_green.png /TextBlock ForegroundGreenGreen/TextBlock/StackPanel/ComboBoxItemComboBoxItemStackPanel OrientationHorizontalImage Source/WpfTutorialSamples;component/Images/bullet_blue.png /TextBlock ForegroundBlueBlue/TextBlock/StackPanel/ComboBoxItem/ComboBox
/StackPanel
数据绑定ComboBox
StackPanel Margin10ComboBox NamecmbColorsComboBox.ItemTemplateDataTemplateStackPanel OrientationHorizontalRectangle Fill{Binding Name} Width16 Height16 Margin0,2,5,2 /TextBlock Text{Binding Name} //StackPanel/DataTemplate/ComboBox.ItemTemplate/ComboBox
/StackPanel
public partial class ComboBoxDataBindingSample : Window
{public ComboBoxDataBindingSample(){InitializeComponent();cmbColors.ItemsSource typeof(Colors).GetProperties();}
}
IsEditable
StackPanel Margin10ComboBox IsEditableTrueComboBoxItemComboBox Item #1/ComboBoxItemComboBoxItemComboBox Item #2/ComboBoxItemComboBoxItemComboBox Item #3/ComboBoxItem/ComboBox
/StackPanel
使用ComboBox选择
StackPanel Margin10ComboBox NamecmbColors SelectionChangedcmbColors_SelectionChangedComboBox.ItemTemplateDataTemplateStackPanel OrientationHorizontalRectangle Fill{Binding Name} Width16 Height16 Margin0,2,5,2 /TextBlock Text{Binding Name} //StackPanel/DataTemplate/ComboBox.ItemTemplate/ComboBoxWrapPanel Margin15 HorizontalAlignmentCenterButton NamebtnPrevious ClickbtnPrevious_Click Width55Previous/ButtonButton NamebtnNext ClickbtnNext_Click Margin5,0 Width55Next/ButtonButton NamebtnBlue ClickbtnBlue_Click Width55Blue/Button/WrapPanel
/StackPanel
public partial class ComboBoxSelectionSample : Window
{public ComboBoxSelectionSample(){InitializeComponent();cmbColors.ItemsSource typeof(Colors).GetProperties();}private void btnPrevious_Click(object sender, RoutedEventArgs e){if(cmbColors.SelectedIndex 0)cmbColors.SelectedIndex cmbColors.SelectedIndex - 1;}private void btnNext_Click(object sender, RoutedEventArgs e){if(cmbColors.SelectedIndex cmbColors.Items.Count-1)cmbColors.SelectedIndex cmbColors.SelectedIndex 1;}private void btnBlue_Click(object sender, RoutedEventArgs e){cmbColors.SelectedItem typeof(Colors).GetProperty(Blue);}private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e){Color selectedColor (Color)(cmbColors.SelectedItem as PropertyInfo).GetValue(null, null);this.Background new SolidColorBrush(selectedColor);}
}