<Button>,<ScrollViewer>,<Label>,<CheckBox>,<RadioButton>,<ToolTip>
1:ContentControl class 有一个类型是object的Content属性,可以是任何东西。eg:button控件的content可以是任何形式的element:(text,text+image, image)和一个ContentTemplate属性可以接收DataTemplate类型的控件。
2:ContentControl的Content属性可以隐式的表达
<Button Margin="4" HorizontalAlignment="Left" Padding="4"> <StackPanel Orientation="Horizontal"> <Image Source="copy.png" Width="16" Height="16"/> <TextBlock Text="Copy" Margin="10,0,0,0" VerticalAlignment="Center" FontSize="16"/> </StackPanel> </Button>
也可以显式的表达
<Button Content="{StaticResource p1}" FontSize="16" Margin="4" Padding="4" HorizontalAlignment="Left"/>
3:有关ContentControl的Content属性设置规则
注意:各类属性不一定只在xaml中才可以设置,也可以在后台.cs中设置,具体在哪里要灵活使用。但如果是在后台设置一个ContentControl的Content属性,则首先要给ContentControl定义一个x:Name="?",然后后台便可以
View Code
Headered ContentControl:<GroupBox>
1:继承自ContentControl,除了已有的Content和ContentTemplate属性外,还多了两个属性(Header type of object, HeaderTemplate type of DataTemplate),4个属性都遵从上面的属性设置规则。
?.Content = ?.Header = new Book { Name = "Windows Internals", Author = "Mark Russinovich", YearPublished = 2009 }; }
这里包括了很多类,我们着重看Selector类
Selector类
1:Items属性:一组object,比较ContentControl的Content属性(一个Object)
2:一些重要的属性:SelectedIndex, SelectedItem, SelectionChanged, SelectedValue & SlectedValuePath
3:SelectedValue & SlectedValuePath: 如果Selector当前hold的是person objects并且SlectedValuePath=Name, SelectedValue是当前选中的Person的Name
ListBox: 继承自Selector类
1:另外有的属性包括:
SelectionMode = Single, Multiple, Extended
SelectedItems
SelectionChanged
2:ListBox的Wrapper<ListBoxItem>(ContentControl)
ComboBox: 继承自Selector类
1:ComboBox的Wrapper<ComboBoxItem>(ContentControl):对于each object added into ListBox/ComboBox时这些wrapper会自动加载
2:<ComboBoxItem>是ContentControl类型,代表可以使用DataTemplate类型创建任意类型的ContentControl
两种方法实现:
1 <Window x:Class="CH04.Lists2.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <Grid.RowDefinitions> 7 <RowDefinition Height="Auto" /> 8 <RowDefinition /> 9 </Grid.RowDefinitions> 10 <ComboBox Margin="4" x:Name="_langCombo" HorizontalContentAlignment="Stretch" 11 SelectionChanged="OnLanguageChanged"> 12 <ComboBoxItem Padding="4"> 13 <StackPanel Orientation="Horizontal"> 14 <Image Source="Images/C++2.jpg" Width="32" Stretch="Uniform" /> 15 <TextBlock Text="C++" VerticalAlignment="Center" FontSize="20" Margin="10,0,0,0" /> 16 </StackPanel> 17 </ComboBoxItem> 18 <ComboBoxItem Padding="4"> 19 <StackPanel Orientation="Horizontal" > 20 <Image Source="Images/CS.jpg" Width="32" Stretch="Uniform" /> 21 <TextBlock Text="C#" VerticalAlignment="Center" FontSize="20" Margin="10,0,0,0" /> 22 </StackPanel> 23 </ComboBoxItem> 24 <ComboBoxItem Padding="4"> 25 <StackPanel Orientation="Horizontal" > 26 <Image Source="Images/VB2.jpg" Width="32" Stretch="Uniform" /> 27 <TextBlock Text="Visual Basic" VerticalAlignment="Center" FontSize="20" Margin="10,0,0,0" /> 28 </StackPanel> 29 </ComboBoxItem> 30 <ComboBoxItem Padding="4"> 31 <StackPanel Orientation="Horizontal" > 32 <Image Source="Images/FS.jpg" Width="32" Stretch="Uniform" /> 33 <TextBlock Text="F#" VerticalAlignment="Center" FontSize="20" Margin="10,0,0,0" /> 34 </StackPanel> 35 </ComboBoxItem> 36 </ComboBox> 37 <GroupBox Header="Some Keywords" Grid.Row="1" Margin="4"> 38 <ListBox x:Name="_keywordList" Margin="4"> 39 </ListBox> 40 </GroupBox> 41 </Grid> 42 </Window>
1:<Image>Tag需要Source属性(ImageSource类型,是Image的具体Data)指定一个url(string类型:xxx.png)文件,能从一个string类型变为ImageSource类型(Image的具体Data),原因是type converter从这段string类型转变成一个BitmapImage(派生自ImageSource类)
2:Stretch属性:
Stretch=Uniform(Default):保持图像比例,但是会缩小在<Image>所设置的width,height里
Stretch=UniformToFill:保持图像比例并且尽量多的占据生育空间,但是多余<Image>所设置的width,height的图像会被切掉
Stretch=None:保留原图像大小多余被切掉,用ScrollViewer可以Scroll着看
Stretch=Fill:改变尺寸比例,只是为了把图像充满<Image>所设置的width,height里
3: ImageSource类有三种派生类可以用(D3DImage,DrawingImage(一些2d drawing实例:<GeometryDrawing>),BitmapSource)
Drawing:
如何创建一个Drawing
<Window x:Class="CH04.ImageSources.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Image Sources" Height="350" Width="525"> <Window.Resources> <CombinedGeometry x:Key="ringGeometry" GeometryCombineMode="Exclude"> <CombinedGeometry.Geometry1> <EllipseGeometry Center="100,100" RadiusX="100" RadiusY="100" /> </CombinedGeometry.Geometry1> <CombinedGeometry.Geometry2> <EllipseGeometry Center="100,100" RadiusX="80" RadiusY="80" /> </CombinedGeometry.Geometry2> </CombinedGeometry> <GeometryDrawing x:Key="ringDrawing" Geometry="{StaticResource ringGeometry}" Brush="LightBlue"> <GeometryDrawing.Pen> <Pen Brush="Black" Thickness="3" /> </GeometryDrawing.Pen> </GeometryDrawing> </Window.Resources> <UniformGrid Rows="1" Columns="2"> <Image> <Image.Source> <DrawingImage Drawing="{StaticResource ringDrawing}" /> </Image.Source> </Image> <Image x:Name="_image"> </Image> </UniformGrid> </Window>
Drawing不是element,不可以直接放在visual tree里,但是可以放在<Image>的Source属性里
BitmapSource:
其本身就是一个抽象类,里面又包含很多类,其中一个是WriteableBitmap
ToolTip可以custmoze
WPF中.cs file中定义的类没有加public可以被同一个porject下的其他.cs或者 .xaml调用,但是记得xaml下要加入project的ns。
xaml可以简单的创建类的实例并赋值属性,一边xaml里随意使用创建好的实例
class Person { public int Age { get; set; } public string Name { get; set; } public override string ToString() { return string.Format("{0} is {1} years old", Name, Age); } }
<local:Person Age="10" Name="Bart" x:Key="p1"/>
xaml中如何StringFormat
<TextBlock Text="{Binding Name, StringFormat=Name: {0}}" /> <TextBlock Text="{Binding Author, StringFormat=Author: {0}}" /> <TextBlock Text="{Binding YearPublished, StringFormat=Published: {0}}" />