Chapter4:Using Standard Control(学习)
ContentControl
<Button>,<ScrollViewer>,<Label>,<CheckBox>,<RadioButton>,<ToolTip>
1:ContentControl class 有一個類型是object的Content屬性,可以是任何東西。eg:button控件的content可以是任何形式的element:(text,text+image, image)和一個ContentTemplate屬性可以接收DataTemplate類型的控件。
2:ContentControl的Content屬性可以隱式的表達
View Code <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>也可以顯式的表達
View Code <Button Content="{StaticResource p1}" FontSize="16"Margin="4" Padding="4" HorizontalAlignment="Left"/>3:有關ContentControl的Content屬性設置規則
- Content屬性如果是string,textblock會被render,textblock的text屬性為這個string
- Content屬性如果隱式表達(不寫Content屬性直接夾在<Button>...</Button>中間)的是一個UIElement object,則該UIElement被render,如果是一行的UIElement(<Image>)則外面什么都不加,如果是多行(<Image>+<TextBlock>)則外面要加一個panel(<Grid>,<StackPanel>,<Border>)
- 如果ContentTemplate屬性是null的話,Content屬性是自定義Object的話,則被渲染成TextBlock with its Text屬性 set to the ToString方法 of the Content屬性值(object類型), otherwise DataTempate被應用
-
注意:各類屬性不一定只在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};}ItemsControl
這里包括了很多類,我們著重看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
兩種方法實現:
- 手動增加每一行ComboBoxItem
- 自動增加具有一定DataTemplate格式的data objects
?
?
?
Display Image
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
?
View Code <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
ToolTip可以custmoze
?
?
?
WPF中.cs file中定義的類沒有加public可以被同一個porject下的其他.cs或者 .xaml調用,但是記得xaml下要加入project的ns。
xaml可以簡單的創建類的實例并賦值屬性,一邊xaml里隨意使用創建好的實例
View Code 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
View Code <TextBlock Text="{Binding Name, StringFormat=Name: {0}}" /><TextBlock Text="{Binding Author, StringFormat=Author: {0}}" /><TextBlock Text="{Binding YearPublished, StringFormat=Published: {0}}" />?
轉載于:https://www.cnblogs.com/shawnzxx/archive/2013/03/11/2953364.html
總結
以上是生活随笔為你收集整理的Chapter4:Using Standard Control(学习)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解析IOS控制台利用GDB命令查看报错堆
- 下一篇: [AHOI2009]最小割(最大流+ta