2009年8月1日

【msdn wpf forum翻译】TextBox中文本 中对齐 的方法

原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49864e35-1dbf-4292-a361-93f1a8400558

问题:
TextBox中文本中对齐,使用 TextBox.HorizontalContentAlignment="Center"行不通(TextBox.VerticalContentAlignment="Center"则会起到预期的作用。)
Number8
Users MedalsUsers MedalsUsers Medals回答:Users MedalsUsers Medals
<Setter Property="TextAlignment" Value="Center"/>

注:参见msdn对 TextBox.TextAlignment 的解释:

TextAlignment 值之一,该值指定文本框内容的水平对齐方式。默认值为 TextAlignment.Left

获取此属性将返回当前对齐方式。 设置此属性将调整文本框的内容以反映指定的对齐方式。
此属性的优先级高于 HorizontalContentAlignment 属性。

 


posted @ 2009-08-01 19:14 caoyang.org 阅读(160) 评论(0) 编辑

【msdn wpf forum翻译】TextBlock等类型的默认样式(implicit style)为何有时不起作用?

摘要: 原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/148e95c6-6fb5-4399-8a56-41d0e0a72f1b疑问:以下代码定义了一个TextBlock的默认样式:[代码]以下是TreeView的一个 HierarchicalDataTemplate 的定义:[代码]之后,就算我们把 TextBlock的默认样式...阅读全文

posted @ 2009-08-01 00:35 caoyang.org 阅读(384) 评论(0) 编辑

2009年7月31日

【msdn wpf forum翻译】获取当前窗口焦点所在的元素

原文地址: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6bd7a03a-f0b4-42df-a7f2-5182cf003cb0

Bialgous回答:
IInputElement focusedElement = FocusManager.GetFocusedElement(thisWindow);
有两点需要注意:
1. 逻辑焦点 != 键盘焦点
    比如当你点开一个菜单时,菜单具有物理焦点
2. 要想检查得到的这个元素是否有键盘焦点,需检查 IsKeyboardFocused 属性,如果它 == true,则这个元素有键盘焦点

注:逻辑焦点、物理焦点的概念是什么?不很清楚,请大家赐教:)  ....

以下是原文:

Hi, you can always determine which element has logical focus in your application through the FocusManager.GetFocusedElement method -- pass it the window in question and it will return which element has logical focus in that window.  Remember that logical focus != keyboard focus at all times -- toolbars and menus track their own focus so if you are currently interacting with a menu then the menu has physical focus.  But in general, the following code will tell you which element WPF thinks has focus in the window:

IInputElement focusedElement = FocusManager.GetFocusedElement(thisWindow);

To determine whether this element has keyboard focus, we can check the IsKeyboardFocused property - if it's set to true, then that element currently has the keyboard focus (as well as being the logical focus for that focus scope).

posted @ 2009-07-31 00:06 caoyang.org 阅读(414) 评论(0) 编辑

2009年7月30日

【msdn wpf forum翻译】如何在wpf程序(程序激活时)中捕获所有的键盘输入,而不管哪个元素获得焦点?

原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cf884a91-c135-447d-b16b-214d2d9e9972


有时有些特殊的程序需要这样处理。

Jim Zhou回答:
在程序启动的时候,可以这样注册:
以下是示例代码:
示例代码


posted @ 2009-07-30 23:18 caoyang.org 阅读(68) 评论(0) 编辑

【msdn wpf forum翻译】在wpf程序中显示一个doc文件的内容

原文链接: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0b76919e-864c-476e-b259-427d73463c71


在DocumentViewer控件中没法直接显示一个 .doc文件 ,  但在wpf程序中显示一个doc文件至少有以下三种
1. 使用 WebBrowser 来显示 .doc文件,如
    this.webBrowser1.Navigate(@"c:\test.doc");
2. 使用DSO Framer control在wpf程序中host .doc文件,参见下文:
    Hosting Office in a WPF Application
    http://www.drwpf.com/blog/Home/tabid/36/EntryID/3/Default.aspx
3. 将 .doc 文件转换为 XPS 文件, 然后使用 DocumentViewer 来显示内容, 参见下文:
    Generate XPS from Word documents with VB 2008 and WPF
    http://www.codeproject.com/KB/office/GenerateXPSfromWordVB.aspx

注:其中1,2两种方法需要机器上安装了word,所以,综上可知,较为完美的显示一个doc文件还是只能靠word的支持

posted @ 2009-07-30 23:01 caoyang.org 阅读(463) 评论(0) 编辑

2007年9月24日

《Applications=Code+Markup》读书笔记 2(第二章 基本画刷)

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 阅读(121) 评论(0) 编辑

《Applications=Code+Markup》读书笔记 1(第一章 初识Application和Window)

知识点

[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 阅读(150) 评论(0) 编辑

2007年9月14日

Bind Enum to Combobox.SelectedIndex

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 阅读(228) 评论(0) 编辑

2007年9月9日

如何捕获正在执行的函数名?

posted @ 2007-09-09 01:32 caoyang.org 阅读(43) 评论(0) 编辑

2007年9月5日

wpf中显示HTML(转自http://steeven.cnblogs.com/archive/2006/06/12/424258.html)

posted @ 2007-09-05 00:54 caoyang.org 阅读(203) 评论(0) 编辑

导航

<2012年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

公告

昵称:caoyang.org
园龄:4年5个月
粉丝:1
关注:1

搜索

 

常用链接

我的标签

随笔分类

随笔档案

WPF

最新评论

阅读排行榜

评论排行榜

推荐排行榜