WPF和XAML的隐藏功能是什么?

123

有很多编程语言中讨论的隐藏功能,现在我好奇XAML和WPF有哪些隐藏功能?

我发现其中一个是ListView的标题点击事件。

<ListView x:Name='lv' 
      Height="150" 
      GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">

GridViewColumnHeader.Click属性没有列出。

迄今为止的一些相关功能:

另请参阅:

  1. C#的隐藏功能
  2. Python的隐藏功能
  3. ASP.NET的隐藏功能
  4. Perl的隐藏功能
  5. Java的隐藏功能
  6. VB.NET的隐藏功能
  7. PHP的隐藏功能
  8. Ruby的隐藏功能
  9. C的隐藏功能
  10. 等等……

7
请看这里:http://msdn.microsoft.com/en-us/library/system.windows.controls.gridviewcolumnheader_events.aspx。点击事件继承自ButtonBase。您所描述的是附加事件,这是WPF中非常强大的一个概念(http://msdn.microsoft.com/en-us/library/bb613550.aspx)。使用附加事件,您可以在网格上使用100个按钮,并且只需一个处理程序。 - Sorskoot
1
起初我想,“哦,又来了”,但是在回复中我学到了一些东西,所以我收回了之前的想法 :o :o - Sam Harwell
1
应该是社区维基 - tsilb
2
@tsilb 我认为它不应该是社区维基,看一下这个链接 http://meta.stackexchange.com/questions/392/should-the-community-wiki-police-be-shut-down - Prashant Cholachagudda
25个回答

87

Multibinding(与StringFormat组合使用):

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat="{}{0}, {1}">
      <Binding Path="LastName" />
      <Binding Path="FirstName" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

1
很棒 :-),除非你正在使用Silverlight 4或更早版本。为v5祈祷。 - Simon_Weaver
5
这很棒,但我会试探一下是否不去做它。如果我需要构建一个字符串,我会把它视为逻辑,并希望对输出进行单元测试。像这样的东西有时最好放在视图模型中作为一个string.Format()。 - Iain Holder

58

还有一个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 

4
在Visual Studio 2010中,您需要将跟踪设置的级别设置为警告!请参阅https://dev59.com/oXE85IYBdhLWcg3wYicB#3004469 - WaltiD

44

3.5sp1 引入了 StringFormat 到绑定表达式中,例如:

<TextBox Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}" />

我无法用言语表达我有多喜欢那个功能。我讨厌周围堆积着大量的值转换器。 - Rob
是的,这是最省时的功能之一。特别是当与TargetNullValue结合使用时,许多问题都会消失。 - Bryan Anderson
6
将StringFormat用单引号括起来可以消除一些编译器警告 - Text={Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}" - Ryan Versaw
好知道,我已经习惯了忽略它们。 - Bryan Anderson
有人能给一个遵循国际化建议的例子吗?我相信日期格式不应该像那样硬编码。 - chillitom
1
我想表达的是任何任意格式的字符串都可以使用。我相信在这种情况下,国际化版本将是StringFormat='{}{0:d}'。 - Bryan Anderson

44

3.5sp1引入了TargetNullValue绑定功能。如果输入值为空,则将绑定属性设置为Null,如果您的属性为Null,则会显示此值。

<TextBox Text="{Binding Total, TargetNullValue=$0.00}" />

29
有时候你会得到一个在标签上显示的字符串过长。这种情况下,我们可以使用TextBlockTextTrimming属性来显示省略号。
<TextBlock 
  Name="sampleTextBlock" 
  TextTrimming="WordEllipsis" 
  TextWrapping="NoWrap"/>

MSDN链接


在这种情况下考虑添加工具提示:http://tranxcoder.wordpress.com/2008/10/12/customizing-lookful-wpf-controls-take-2/ - surfen

27

给窗口添加Aero效果

  <Window.Resources>
    <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, 
        PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</Window.Resources>

1
已添加了代码,但仍未添加 Aero 效果。我有遗漏什么吗? - Elmo

21

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

1
很遗憾,x:TypeArguments 只能在松散的 XAML 文件中使用,而不能在编译后的文件中使用 :( - kevindaub
是的,只有松散的XAML :( 对于大多数WPF开发人员来说,XAML2009是无用的。 - Grigory

19

在禁用的控件上显示工具提示

WPF 允许在一个控件处于禁用状态时显示工具提示。

例如:

<Button Content="Disabled Button" ToolTipService.ShowOnDisabled="True" IsEnabled="False" ToolTip="This is a disabled button"/> 

19

使用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>

18

标记扩展和附加属性是我最喜欢的功能,它们能够以非常优雅的方式扩展 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
是的,但自从我发布这篇文章以来它已经发生了很大变化...现在有两个属性,EnableBlur和GlassFrameMargins。你可以在这里找到代码:http://projets.developpez.com/projects/dvp-net/repository/entry/trunk/src/Developpez.Dotnet.Windows/Util/DwmUtil.cs - Thomas Levesque

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接