随笔 - 5  文章 - 0 评论 - 0 trackbacks - 0
<2009年1月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

与我联系

搜索

 

常用链接

留言簿

我参加的小组

我参与的团队

随笔档案

最新评论

阅读排行榜

评论排行榜

Color

可以new出来,也可以通过它的静态方法Color.FromRgb(r,g,b)或Color.FromArgb(a,r,g,b)获得

Color是一个struct.

System.Window.Media命名空间下还有一些Colors的类,包含141个static的只读的属性 (whose names begin alphabetically with AliceBlue and AntiqueWhite and conclude with Yellow and YellowGreen).

Brush的继承关系

其中本章介绍SolidColorBrush和GradientBrush (abstract) (以及其子类LinearGradientBrush RadialGradientBrush). 以下为类继承关系图:

Object
    DispatcherObject (abstract)
          DependencyObject
                Freezable (abstract)
                       Animatable (abstract)
                             Brush (abstract)
                                   GradientBrush (abstract)
                                         LinearGradientBrush
                                         RadialGradientBrush
                                   SolidColorBrush
                                   TileBrush (abstract)
                                          DrawingBrush
                                          ImageBrush
                                          VisualBrush

Brush广泛地应用于Control的Background等涉及到颜色的属性

在WPF中,颜色Color是一个单纯的结构,而Brush才是色彩的主演。

如何构造一个SolidColorBrush呢?

  1. SolidColorBrush的构造函数接收一个参数Color,而Color可通过本章最初介绍的方法获得。
  2. SolidColorBrush有一个属性 Color,可通过赋值改变SolidColorBrush的颜色。
  3. Brushes有和Colors对应的141种预定义的单色,但不能直接赋值:
    //get an Invalid Operation Exception that states 
    //"Cannot set a property on object '#FF000000' because it is in a read-only state." 
    SolidColorBrush brush = Brushes.Black;
    //可以这样:
    SolidColorBrush brush = Brushes.Black.Clone();

注:以上第三点,究其原因,是因为Brushes的静态变量Black已经frozen了。

The SolidColorBrush objects returned from the Brushes class are in a frozen state,
which means they can no longer be altered.
Like the Changed event, freezing is implemented in the Freezable class, from which Brush inherits.
If the CanFreeze property of a Freezable object is true,
it's possible to call the Freeze method to render the object frozen and unchangeable.
The IsFrozen property indicates this state by becoming true.
Freezing objects can improve performance because they no longer need to be monitored for changes.
A frozen Freezable object can also be shared across threads, while an unfrozen Freezable object cannot.
Although you cannot unfreeze a frozen object, you can make an unfrozen copy of it.

系统画刷以及其优点

SystemColors与Brushes和Colors相似,例如,它可以这样获取颜色及画刷:

SystemColors.WindowColorBrush和SystemColors.WindowColor 也可以这样修改画刷:

Brush brush = new SystemColorBrush(SystemColors.WindowColor);

GradientBrush

包括LinearGradientBrush和RadialGradientBrush

LinearGradientBrush 线性梯度渐变画刷

介绍了2种构造函数

  1. new LinearGradientBrush(Colors.Red, Colors.Blue, new Point(0, 0), new Point(1, 1));
  2. new LinearGradientBrush(clr1, clr2, angle);

SpreadMethod 、GradientStops、StartPoint、EndPoint等属性

  1. 通过设置这些属性,可以改变GradientBrush的样子。

RadialGradientBrush 径向梯度渐变画刷

与LinearGradientBrush相仿,只是色彩渐变的方向是径向

作者给出了几个很有趣的例子,形象地说明了这些Brush的特点。

本章最后介绍了Control类的属性BorderBrush、BorderThickness、Foreground

并引出了下一章:The Concept of Content

posted @ 2007-09-24 23:56 caoyang.org 阅读(77) | 评论 (0)编辑

知识点

[STAThread] WPF程序的Main函数必须声明为[STAThread]:

In any WPF program, the [STAThread] attribute must precede Main or the C# compiler will complain.
This attribute directs the threading model of the initial application thread to be a single-threaded apartment,
which is required for interoperability with the Component Object Model (COM).
"Single-threaded apartment" is an old COM-era, pre-.NET programming term,
but for our purposes you could imagine it to mean
our application won't be using multiple threads originating from the runtime environment.

app.Run();开始进入消息循环,可带参数(win)

介绍Application中的事件

包括Startup, SessionEnding 等 以及 可override的函数(default event handler) OnStartup , OnSessionEnding

Window 中的事件(继承自UIElement)(TextInput,MouseDown等)

Following initialization, virtually everything a program does is in response to an event.
These events usually indicate keyboard, mouse, or stylus input from the user.
The UIElement class (which refers to the user interface, of course) defines a number of keyboard-,
mouse-, and stylus-related events; the Window class inherits all those events.
One of those events is named \MouseDown. A window's \MouseDown event occurs 
whenever the user clicks the client area of the window with the mouse.

Window的一些属性(Height,Width,Top,Left; Title; WindowStartupLocation; WindowStyle; ResizeMode;WindowState )

由Background 引出Brushes(见下一章)

关键

熟悉Application和Window的事件

在需要的情况下可继承Window以及Application

在子类中override其中的一些event handler, 修改一些属性

一个WPF程序只有一个Application(对象),这个Application可创建多个Window

这些Window的各种属性都可以通过程序控制.

Window可以以Show()和ShowDialog()两种方式Show出来,弄清二者的区别

隐藏一个Window可以用Hide(),关闭则调用它的Close();

posted @ 2007-09-24 22:02 caoyang.org 阅读(53) | 评论 (0)编辑

Do you mean that you want to bind a variable (not a property) to ComboBox.SelectedIndex? If so, I think that the better method to do this is bind a property to ComboBox.SelectedItem. The following example shows how to bind a property to the ComboBox.SelectedItem. If you want to have more functionality from the binding, you should have the property is a dependency property.

 

Code Snippet

 

<Window x:Class="LearningSolution.MainWindow"

xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:system
="clr-namespace:System;assembly=mscorlib"

xmlns:local
="clr-namespace:LearningSolution"

Name
="TheWindow">

<Window.Resources>

<ObjectDataProvider x:Key="EnumValues"

MethodName
="GetValues"

ObjectType
="{x:Type system:Enum}">

<ObjectDataProvider.MethodParameters>

<x:Type TypeName="local:LevelEnum" />

</ObjectDataProvider.MethodParameters>

</ObjectDataProvider>

<DataTemplate x:Key="EnumValueTemplate">

<TextBlock Text="{Binding}"/>

</DataTemplate>

</Window.Resources>

<StackPanel>

<ComboBox ItemsSource="{Binding Source={StaticResource EnumValues}}"

ItemTemplate
="{StaticResource EnumValueTemplate}"

SelectedItem
="{Binding ElementName=TheWindow, Path=TheLevel, Mode=OneWayToSource}"/>

<Button Click="Button_Click">Watch</Button>

</StackPanel>

</Window>

public partial class MainWindow : Window

{

public LevelEnum TheLevel { get; set; }

private void Button_Click(object sender, RoutedEventArgs e)

{

MessageBox.Show(this.TheLevel.ToString());

}

}

public enum LevelEnum { None = 0, First = 1, Second = 2, Third = 3 }

 

 

 

 

For more information about binding and dependency property, you could refer to the following links.

http://msdn2.microsoft.com/en-us/library/ms753192.aspx

http://msdn2.microsoft.com/en-us/library/ms750612.aspx

posted @ 2007-09-14 01:07 caoyang.org 阅读(110) | 评论 (0)编辑
可以使用创建一个StackFrame类, 然后通过GetMethod方法来获取一个方法的信息. 如下:
MessageBox.Show("The current method name is :" + new System.Diagnostics.StackFrame().GetMethod().Name);
posted @ 2007-09-09 01:32 caoyang.org 阅读(22) | 评论 (0)编辑
真是匪夷所思,目前版本居然不支持
从论坛查到有两个方法:
1. 如果有url, 用frame
2. 如果没有,需要在用到winform和wpf的互操作,就是嵌入winform的Webbrowser控件。
需要引入两个dll,xaml如下:
  <windowsformshost ClipToBounds="True" Opacity="1" Foreground="{x:Null}" Height="Auto" VerticalAlignment="Stretch" Grid.RowSpan="1" Grid.Row="1" x:Name="WindowsFormsHost" Margin="0,51.3214285714285,0,0">
<wf:webbrowser x:Name="webBrowser"></wf:webbrowser>
</windowsformshost>
posted @ 2007-09-05 00:54 caoyang.org 阅读(76) | 评论 (0)编辑