有很多编程语言中讨论的隐藏功能,现在我好奇XAML和WPF有哪些隐藏功能?
我发现其中一个是ListView的标题点击事件。
<ListView x:Name='lv'
Height="150"
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
GridViewColumnHeader.Click属性没有列出。
迄今为止的一些相关功能:
另请参阅:
有很多编程语言中讨论的隐藏功能,现在我好奇XAML和WPF有哪些隐藏功能?
我发现其中一个是ListView的标题点击事件。
<ListView x:Name='lv'
Height="150"
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
GridViewColumnHeader.Click属性没有列出。
迄今为止的一些相关功能:
另请参阅:
Multibinding(与StringFormat组合使用):
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="LastName" />
<Binding Path="FirstName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
还有一个PresentationTraceSources.TraceLevel小技巧,可以调试任何特定情况下绑定的工作情况。您所需要做的就是在WindowsBase程序集中引用System.Diagnostics命名空间。
xmlns:sd="clr-namespace:System.Diagnostics;assembly=WindowsBase"
然后将以下内容添加到绑定表达式中:
<TextBlock Text="{Binding Message, sd:PresentationTraceSources.TraceLevel=High}" />
日志将会是这样:
System.Windows.Data Warning: 52 : Created BindingExpression (hash=5923895) for Binding (hash=7588182)
System.Windows.Data Warning: 54 : Path: 'Message'
System.Windows.Data Warning: 56 : BindingExpression (hash=5923895): Default mode resolved to OneWay
System.Windows.Data Warning: 57 : BindingExpression (hash=5923895): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 58 : BindingExpression (hash=5923895): Attach to System.Windows.Controls.TextBlock.Text (hash=65248697)
System.Windows.Data Warning: 63 : BindingExpression (hash=5923895): Resolving source
3.5sp1 引入了 StringFormat 到绑定表达式中,例如:
<TextBox Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}" />
Text={Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}"
- Ryan Versaw3.5sp1引入了TargetNullValue绑定功能。如果输入值为空,则将绑定属性设置为Null,如果您的属性为Null,则会显示此值。
<TextBox Text="{Binding Total, TargetNullValue=$0.00}" />
TextBlock
的TextTrimming
属性来显示省略号。<TextBlock
Name="sampleTextBlock"
TextTrimming="WordEllipsis"
TextWrapping="NoWrap"/>
<Window.Resources>
<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</Window.Resources>
XAML中使用x:TypeArguments的泛型
如果希望在XAML中使用ObservableCollection,则需要创建一个从ObservableCollection派生的类型,因为它不能在XAML中声明。使用XAML 2009,可以使用x:TypeArguments属性来定义泛型类型的类型。
<!-- XAML 2006 -->
class EmployeeCollection : ObservableCollection<Employee>
{
}
<l:EmployeeCollection>
<l:Employee FirstName="John" Name="Doe" />
<l:Employee FirstName="Tim" Name="Smith" />
</lEmployeeCollection>
<!-- XAML 2009 -->
<ObservableCollection x:TypeArguments="Employee">
<l:Employee FirstName="John" Name="Doe" />
<l:Employee FirstName="Tim" Name="Smith" />
</ObservableCollection />
在禁用的控件上显示工具提示
WPF 允许在一个控件处于禁用状态时显示工具提示。
例如:
<Button Content="Disabled Button" ToolTipService.ShowOnDisabled="True" IsEnabled="False" ToolTip="This is a disabled button"/>
使用x:Arguments与非默认构造函数
在XAML 2006中,对象必须具有公共默认构造函数才能使用它们。在XAML 2009中,您可以使用x:Arguments语法传递构造函数参数。
<!-- XAML 2006 -->
<DateTime>00:00:00.0000100</DateTime>
<!-- XAML 2009 -->
<DateTime>
<x:Arguments>
<x:Int64>100</x:Int64>
</x:Arguments>
</DateTime>
标记扩展和附加属性是我最喜欢的功能,它们能够以非常优雅的方式扩展 XAML 的“词汇”。
标记扩展
<!-- Binding to app settings -->
<CheckBox IsChecked="{my:SettingBinding MinimizeToTray}">Close to tray</CheckBox>
<!-- Fill ItemsControl with the values of an enum -->
<ComboBox ItemsSource="{my:EnumValues sys:DaysOfWeek}"/>
<!-- Localization -->
<TextBlock Text="{my:Localize HelloWorld.Text}"/>
<!-- Switch on the result of a binding -->
<TextBlock Text="{my:Switch Path=IsGood, ValueIfTrue=Good, ValueIfFalse=Bad}"/>
附加属性
<!-- Sort GridView automatically -->
<ListView ItemsSource="{Binding Persons}"
IsSynchronizedWithCurrentItem="True"
util:GridViewSort.AutoSort="True">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Name}"
util:GridViewSort.PropertyName="Name"/>
<GridViewColumn Header="First name"
DisplayMemberBinding="{Binding FirstName}"
util:GridViewSort.PropertyName="FirstName"/>
<GridViewColumn Header="Date of birth"
DisplayMemberBinding="{Binding DateOfBirth}"
util:GridViewSort.PropertyName="DateOfBirth"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<!-- Vista Glass effect -->
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1"
my:WinUtil.EnableAeroGlass="True">
...
这是来自GridViewSort的源代码(顺便说一下,它使用了Ortus提到的GridViewColumnHeader.Click
事件)
WinUtil.EnableAeroGlass
的源代码能在某个地方获取到吗? - Oskar