1、后台打开网页
1 private void OpenWeb(object sender, RoutedEventArgs e) 2 { 3 System.Diagnostics.Process.Start("http://www.cnblogs.com/submarine"); 4 }
2、窗口间跳转
1 private void LogInSucc(object sender, RoutedEventArgs e) 2 { 3 this.Hide(); 4 OtherWindow win = new OtherWindow(); 5 win.Show(); 6 }
3、垂直分割线()
1 <Separator Height="5" HorizontalAlignment="Left" Margin="176,8,0,0" Name="separator2" VerticalAlignment="Top" Width="120" > 2 <Separator.LayoutTransform> 3 <RotateTransform Angle="90" /> 4 </Separator.LayoutTransform> 5 </Separator>
4、Button中带图片和文字
1 <Button Height="36" HorizontalAlignment="Left" Margin="26,0,0,0" Name="button3" VerticalAlignment="Top" Width="110" Click="OpenWeb"> 2 <StackPanel Height="24" HorizontalAlignment="Left" Margin="0,0,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="90" > 3 <Image Height="24" Name="image1" Stretch="Fill" Width="89" Source="/XX;component/Images/logo.png" /> 4 </StackPanel> 5 </Button>
5、窗口大小不可变
ResizeMode ->CanResize
6、页面间跳转及传值
思路来自:http://archive.msdn.microsoft.com/wpfsamples
背景:根据选择不同的按钮,在窗口A中的某块区域显示相应的内容。
决解方案:在窗口A中的该区域使用控件“Frame”,对应不同的内容页创建相应的页文件,在控件“Frame”的属性Source中选择相对应的页文件的相对路径及全名。
假设窗口A,控件Frame为myFrame,页文件B.xaml。
在事件中:
1 myFrame.Navigate(new Uri("\\B.xaml", UriKind.Relative));
Navigate()异步导航到URI指定的内容。
UriKind.Relative用于指定相对路径,而不是绝对路径。
在于Grid的行列定义。
Auto:给予行或列等同于它所包含组件的大小;
*:共享其他(包括也被指定为*的)行或列余下的可用空间,*=1*;
1 <Grid.RowDefinitions> 2 <RowDefinition Height="*" /> 3 <RowDefinition Height="2*" /> 4 <RowDefinition Height="3*" /> 5 <RowDefinition Height="Auto" /> 6 </Grid.Definitions>
r0~r2共享r3(指定为Auto)余下的空间,r0得到1/6大小,r1得到2/6大小,r2得到3/6大小。
9、ScrollViewer 只能有一个子级,通常可为 Panel 元素,该元素可承载元素的 Children 集合。ScrollViewer 概述
10、ListView静态显示数据
1 <ListView Margin="1,58,-1,180" Grid.Row="1" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 3 xmlns:sys="clr-namespace:System;assembly=mscorlib"> 4 <ListView.View> 5 <GridView> 6 <GridViewColumn Header="AA " 7 DisplayMemberBinding="{Binding [0]}" /> 8 <GridViewColumn Header="BB" 9 DisplayMemberBinding="{Binding [1]}" /> 10 <GridViewColumn Header="CC" 11 DisplayMemberBinding="{Binding [2]}" /> 12 <GridViewColumn Header="DD" 13 DisplayMemberBinding="{Binding [3]}" /> 14 <GridViewColumn Header="EE" 15 DisplayMemberBinding="{Binding [4]}" /> 16 <GridViewColumn Header="FF" 17 DisplayMemberBinding="{Binding [5]}" /> 18 </GridView> 19 </ListView.View> 20 <ListViewItem> 21 <x:Array Type="sys:String" > 22 <sys:String>00</sys:String> 23 <sys:String>01</sys:String> 24 <sys:String>02</sys:String> 25 <sys:String>03</sys:String> 26 <sys:String>04</sys:String> 27 <sys:String>05</sys:String> 28 29 </x:Array> 30 </ListViewItem> 31 <ListViewItem> 32 <x:Array Type="sys:String" > 33 <sys:String>10</sys:String> 34 <sys:String>11</sys:String> 35 <sys:String>12</sys:String> 36 <sys:String>13</sys:String> 37 <sys:String>14</sys:String> 38 <sys:String>15</sys:String> 39 </x:Array> 40 </ListViewItem> 41 </ListView>
11、页之间实现导航功能
苦苦想了N久才想起这个东西叫导航。
通过前进后退两个按钮进行导航操作。
待实现。
12、After move the GridSplitter, the layout don't work correctly
========================================================================================
13、阻止事件路由
举个例子:
1 <Window x:Class="WpfApplication6.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" 5 Height="500" 6 Width="525"> 7 <Grid> 8 <Grid.ColumnDefinitions> 9 <ColumnDefinition Width="Auto" 10 Name="BaseExpanderCol" 11 MinWidth="25"/> 12 <ColumnDefinition Width="Auto"/> 13 <ColumnDefinition Width="*" /> 14 </Grid.ColumnDefinitions> 15 <!--BaseExpander--> 16 <Expander HorizontalAlignment="Stretch" 17 IsExpanded="True" 18 Name="BaseExpander" 19 VerticalAlignment="Stretch" 20 ExpandDirection="Right" 21 BorderBrush="#FFC8C8C8" 22 Expanded="ExpandBaseExpander" 23 Collapsed="CollapseBaseExpander"> 24 <Grid> 25 <!--TreeView in BaseExpander--> 26 <TreeView Height="482" 27 Width="201" 28 Margin="2,29,0,0" 29 HorizontalAlignment="Left" 30 VerticalAlignment="Top" 31 Name="treeView1" 32 BorderBrush="White"> 33 <TreeViewItem Header="item1"> 34 <TreeViewItem Header="1" 35 IsSelected="False"> 36 <TreeViewItem Header="11" /> 37 <TreeViewItem Header="12" /> 38 </TreeViewItem> 39 <TreeViewItem Header="2"> 40 <TreeViewItem Header="21"> 41 <TreeViewItem Header="221"> 42 <TreeViewItem Header="2211" /> 43 </TreeViewItem> 44 </TreeViewItem> 45 </TreeViewItem> 46 </TreeViewItem> 47 </TreeView> 48 <!--ChildExpander in BaseExpander--> 49 <Expander Header="ChildExp" 50 HorizontalAlignment="Left" 51 IsExpanded="True" 52 Name="ChildExpander" 53 VerticalAlignment="Bottom" 54 ExpandDirection="Up" 55 Width="203" 56 BorderBrush="#FFD00000" 57 Collapsed="CollapseChildExpander"> 58 <Grid VerticalAlignment="Bottom" 59 Height="300" 60 Background="White"> 61 <Label Content="开发中..." 62 Height="28" 63 HorizontalAlignment="Left" 64 Margin="6,6,0,0" 65 Name="label1" 66 VerticalAlignment="Top" /> 67 </Grid> 68 </Expander> 69 </Grid> 70 </Expander> 71 <!--GridSplitter--> 72 <GridSplitter Grid.Column="1" 73 HorizontalAlignment="Center" 74 VerticalAlignment="Stretch" 75 Width="6"/> 76 <!--Right Content--> 77 <Grid Grid.Column="2"> 78 </Grid> 79 </Grid> 80 </Window>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace WpfApplication6 16 { 17 /// <summary> 18 /// MainWindow.xaml 19 /// </summary> 20 public partial class MainWindow : Window 21 { 22 private const double cdExpanderCollapsedMaxSize = 25.0; 23 private const double cdBaseExpanderDefaultWidth = 230.0; 24 25 // Width of BaseExpander when expanede 26 private double dBaseExpandedWidth; 27 28 public MainWindow() 29 { 30 // set default size of Expander 31 dBaseExpandedWidth = cdBaseExpanderDefaultWidth; 32 33 InitializeComponent(); 34 } 35 36 private void ExpandBaseExpander(object sender, RoutedEventArgs e) 37 { 38 // ServerExpanderCol is 0th ColumnDefinition 39 // Restore the Width of 0th Col 40 BaseExpanderCol.Width = new GridLength(dBaseExpandedWidth); 41 // Restore the MaxWidth of 0th COl 42 BaseExpanderCol.MaxWidth = Double.PositiveInfinity; 43 } 44 45 private void CollapseBaseExpander(object sender, RoutedEventArgs e) 46 { 47 Expander expd = sender as Expander; 48 // Save the Width of the Expander 49 dBaseExpandedWidth = ((FrameworkElement)expd).ActualWidth; 50 // Set 0th ColDefinition 51 BaseExpanderCol.Width = new GridLength(1, GridUnitType.Auto); 52 // Forbid to move GridSplitter 53 BaseExpanderCol.MaxWidth = cdExpanderCollapsedMaxSize; 54 } 55 56 private void CollapseChildExpander(object sender, RoutedEventArgs e) 57 { 58 59 } 60 } 61 }
1 // 修改后 2 private void ExpandBaseExpander(object sender, RoutedEventArgs e) 3 { 4 // like this 5 if (sender != e.OriginalSource) 6 return; 7 // like this 8 // ServerExpanderCol is 0th ColumnDefinition 9 // Restore the Width of 0th Col 10 BaseExpanderCol.Width = new GridLength(dBaseExpandedWidth); 11 // Restore the MaxWidth of 0th COl 12 BaseExpanderCol.MaxWidth = Double.PositiveInfinity; 13 } 14 15 private void CollapseBaseExpander(object sender, RoutedEventArgs e) 16 { 17 // like this 18 if (sender != e.OriginalSource) 19 return; 20 // like this 21 Expander expd = sender as Expander; 22 // Save the Width of the Expander 23 dBaseExpandedWidth = ((FrameworkElement)expd).ActualWidth; 24 // Set 0th ColDefinition 25 BaseExpanderCol.Width = new GridLength(1, GridUnitType.Auto); 26 // Forbid to move GridSplitter 27 BaseExpanderCol.MaxWidth = cdExpanderCollapsedMaxSize; 28 } 29 30 private void CollapseChildExpander(object sender, RoutedEventArgs e) 31 { 32 // or add it 33 // e.Handled = true; 34 }
1 <Window x:Class="WpfApplication2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:sys="clr-namespace:System;assembly=mscorlib" 5 Title="MainWindow" Height="350" Width="525"> 6 7 <Grid> 8 <ListView Grid.Row="2"> 9 <ListView.ItemContainerStyle> 10 <Style TargetType="ListViewItem"> 11 <Setter Property="HorizontalContentAlignment" 12 Value="Stretch" /> 13 </Style> 14 </ListView.ItemContainerStyle> 15 <ListView.Resources> 16 <Style TargetType="{x:Type CheckBox}" 17 x:Key="DataGridCheckBox"> 18 <Setter Property="HorizontalAlignment" 19 Value="Center" /> 20 <Setter Property="Margin" 21 Value="4" /> 22 <Setter Property="VerticalAlignment" 23 Value="Center" /> 24 <Setter Property="Width" 25 Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" /> 26 </Style> 27 </ListView.Resources> 28 <ListView.View> 29 <GridView> 30 <GridViewColumn Header="Comment"> 31 <GridViewColumn.HeaderTemplate> 32 <DataTemplate> 33 <CheckBox Style="{StaticResource DataGridCheckBox}" /> 34 </DataTemplate> 35 </GridViewColumn.HeaderTemplate> 36 <GridViewColumn.CellTemplate> 37 <DataTemplate> 38 <CheckBox Style="{StaticResource DataGridCheckBox}" /> 39 </DataTemplate> 40 </GridViewColumn.CellTemplate> 41 </GridViewColumn> 42 </GridView> 43 </ListView.View> 44 <ListViewItem /> 45 <ListViewItem /> 46 </ListView> 47 </Grid> 48 </Window>
15、页面之间导航
比如在Main窗口中有一个Frame控件,用于显示Page1和Page2,目的是从Page1切换到Page2。设置Frame的初始内容页为Page1,Page1中有宜Button,为该Button创建Click事件,在该事件中编写:
NavigationService.Navigate(new Page2());
16、寻找控件
17、选择全部CheckBox
18、How to create a right-click context menu for a button in WPF
19、设置Style并使用
20、自定义CheckBox控件,不能给其中一个TextBlock控件设置Foreground。
在模型中对应变量的类型由Color改为string,在该变量的get方法中判断如果没有设置值则默认为黑色。
21、绑定RadioButton控件的IsChecked属性到其他控件的IsEnable属性。
当选择一个RadioButton控件时,对应的其他控件启用。
最初我是通过RadioButton控件的事件进行修改对应控件的IsEnable属性,然而在页对象(或者窗口对象,用于包含这些控件)初始化的时候,会出现异常,即对应的控件还没被创建。
21、延伸:绑定RadioButton控件的IsChecked属性到其他控件的Visibility属性。
22、自定义Converter。
举个例子:当ToggleButton选中时,Label不启用,反之,则启用。
首先建一个继承自IValueConverter的自定义类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Data; 6 7 namespace XXX 8 { 9 public class NegationValueConverter : IValueConverter 10 { 11 12 public object Convert( 13 object value, 14 Type targetType, 15 object parameter, 16 System.Globalization.CultureInfo culture) 17 { 18 return !(bool)value; 19 } 20 21 public object ConvertBack( 22 object value, 23 Type targetType, 24 object parameter, 25 System.Globalization.CultureInfo culture) 26 { 27 throw new NotImplementedException(); 28 } 29 } 30 }
.xaml
1 <Window 2 ... 3 xmlns:local="clr-namespace:XXX" 4 ...> 5 <Window.Resources> 6 <local:NegationValueConverter x:Key="NegationValueConverter"/> 7 </Window.Resources> 8 <StackPanel> 9 <ToggleButton 10 x:Name="NewToggleButton" 11 Content="Hehe" /> 12 <Label 13 IsEnabled="{Binding 14 Path=IsChecked, 15 ElementName=NewToggleButton, 16 Converter={StaticResource NegationValueConverter}}" 17 Content="Hehehehe"/> 18 </StackPanel> 19 </Window>
在Resources集合中创建一个转换器对象是为了避免在每个绑定中都创建一个转换器实例。
控件:
Canvas:定义一个区域,在此区域内,您可以使用相对于 Canvas 区域的坐标显式定位子元素。
DockPanel:定义一个区域,在此区域中,您可以使子元素互相水平或垂直排列。
Grid:定义由行和列组成的灵活网格区域。
StackPanel:将子元素排列成一行(可沿水平或垂直方向)。
VirtualizingPanel:为“虚拟化”其子数据集合的 Panel 元素提供一个框架。这是一个抽象类。
WrapPanel:从左至右按顺序位置定位子元素,在包含框的边缘处将内容断开至下一行。后续排序按照从上至下或从右至左的顺序进行,具体取决于 Orientation 属性的值。对于其所需应用程序布局不可能使用任意预定义的 Panel 元素来实现的方案,您可以通过从 Panel 继承、并重写 MeasureOverride 和 ArrangeOverride 方法来实现自定义布局行为。
—EOF—