0
点赞
收藏
分享

微信扫一扫

WPF-控件的常用属性-单例-隧道事件

guanguans 2023-01-30 阅读 47


特殊字符

小于​​<​

大于​​>​

空格​​xml:space="preserve"​

例如:​​TextBox Grid.Column="1" xml:space="preserve">aaaaa .</TextBox>​

从一个xaml文件中获取xaml内容

//DependencyObject是wpf控件的基类,它可以放到任何类型容器中
DependencyObject rootElement;
using (FileStream fs = new FileStream(xamlFile,FileMode.Open))
{
rootElement = (DependencyObject)XamlReader.Load(fs);
}
this.Content = rootElement;
//根据name获取xaml中的button
Button button = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "btnName");
button += buttonClick;

隧道事件

隧道路由事件,方向与冒泡路由事件相反,从最外层往里层。隧道事件一般和冒泡事件成对出现,只不过事件名称前面加上Preview。

隧道事件的可以先使用preview*Down截获。隧道事件总是在冒泡事件触发前触发。例如:

键盘事件

隧道-冒泡

PreviewKeyDown-KeyDown

PreviewTextInput-TextInput

PreviewKeyUp-KeyUp

等等

鼠标事件

MouseEnter\MouseLeave直接事件

PreviewMouseMove\MouseMove隧道或者路由事件
鼠标单击事件其实是捕捉到鼠标按键释放事件
Mouse.Capture鼠标捕获,当鼠标被捕获时,鼠标就不能点击其他位置
DrapDrop.DoDragDrop()可以设置拖动事件(如拖动textbox中的文本到另一个TextBox)

字体

可以将字体加入资源中,FontFamily=“./#bayern”(不用写tff扩展名)
当字体过小时,可能会出现不清晰的情况如字体大小小于15,可以使用TextOptions.TextFormattingMode="Display"来设置

控件

使用_作为快捷点,如_A
Label有一个Target属性,可以快速链接到某个控件​​​<Lable Target="{Binding ElementName=txtBox}">Choose _A</Label>​​,也就是快速把焦点定位在某个控件上

Button

IsCancel属性,当为true时,按住ESC键会执行
IsDefault属性,当为true时,按住Enter键会执行

Window

WindowStartupLocation
WindowStyle
AllowsTransparency

RadioButton

要设定GroupName

ToolTip

任何控件都可以使用ToolTip属性
复杂的ToolTip可以使用控件的嵌套属性​​​<Button.ToolTip>​​​ ToolTip设置提示出现的位置​​<ToolTip Placement="Mouse" HorizontalOffset="25">​​ 以鼠标为基准,水平偏移25

Popup

设置IsOpen属性来打开或者关闭

ScrollViewer

CanContentScroll="True"根据元素滚动,而不是根据行

Expander

属于带有Header的内容控件

ExpandDirection属性决定向哪里扩展

WPF-控件的常用属性-单例-隧道事件_ui

TextBox

TextWrapping属性:可自动折行
MaxLength属性:最大长度
SelectionStart选中文本开始的位置
SelectionLength:选中长度
SelectionText:选中文本
SpellCheck.IsEnable拼写检查:不支持中文

Slider

TickPlacement属性设置显示刻度

IsSelectionRangeEnable设置某个范围(如背景色不同)

WPF-控件的常用属性-单例-隧道事件_wpf_02

ProgressBar

IsIndeterminate属性是否有脉冲动画

Calendar

DisplayMode
DisplayDateStart
DisplayDateEnd
SelectedMode范围

UniformGrid

提供一种在网格(网格中的所有单元格都具有相同的大小)中排列内容的方法, 相当于简化版本的Grid

Application

在App.xaml中,设置ShutdownMode属性可以设置关闭Application的方式

事件

Startup在调用run之后,主窗口显示之前触发
Exit事件在关闭之前触发
SessionEnding事件在Windows对话结束的时候触发,如注销用户,关闭计算机
DispatcherUnhandledException在有未处理的异常时触发

显示初始界面

将图片设置为SplashScreen

读取当前Application对象

Application.Current.MainWindow

单例

  1. 引用Microsoft.VisualBasic
  2. 创建一个继承自ApplicationServices.WindowsFormsApplicationBase类SignalApplicationServices
  3. 在该类的构造函数中设置this.IsSingleInstance=true
  4. 重写该类的OnStartup方法

WPFApp app
app = new WPFApp();
app.Run();
return false;

  1. 重写onStartupNextInstance方法
    ​​​app.showWindow​
  2. 在Main函数中

SignalApplicationServices app = new SignalApplicationServices();
app.run();

绑定到非元素对象

Source属性
RelativeSource
DataContext

资源

定义资源一定要在使用资源之前
Application并不是资源的最后一级,系统中的资源才是,使用三个类
SystemColor
SystemFont
SystemParmams
如:​​​<Button Foreground="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>​

资源字典

新建WPF资源字典
使用资源字典
必须创建ResourceDictionary

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

然后正常使用

程序集共享资源字典

第一种方法

ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("类库名;component/资源字典文件.xaml", UriKind.Relative);
this.btn.BackGround = (Brush)rd["key"];

第二种方法
类库中的资源必须放在Themes文件夹下,且资源字典名称为generic.xaml必须这样定义

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp6"
>
<ImageBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:类名},ResourceId=资源id}" ImageSource="333.png"></ImageBrush>
</ResourceDictionary>

使用资源时,必须是动态资源

<Button Background={DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type 引入的名称空间.类名},ResourceId=资源ID}}/>

Style

样式事件

<Style>
<EventSetter Event="FrameworkElement.MouseEnter" Handler="FrameworkElement_MouseEnter"/>
</Style>

基于其他样式

​<Style BasedOn={StaticResource BaseStyle}>​

形状

使用Virebox控件缩放形状


举报

相关推荐

0 条评论