日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Chapter4:Using Standard Control(学习)

發布時間:2023/12/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中設置,具體在哪里要靈活使用。但如果是在后臺設置一個ContentControlContent屬性,則首先要給ContentControl定義一個x:Name="?",然后后臺便可以

    View Code

?

Headered ContentControl:<GroupBox>

1:繼承自ContentControl,除了已有的Content和ContentTemplate屬性外,還多了兩個屬性(Header type of objectHeaderTemplate 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
View Code 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>
  • 自動增加具有一定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(学习)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。