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}}" />

 

posted @ 2013-03-11 03:46  若愚Shawn  阅读(303)  评论(0编辑  收藏  举报