WPF 和 XAML 的隐藏功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1124769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:43:38  来源:igfitidea点击:

Hidden features of WPF and XAML?

wpfxamlhidden-features

提问by Sauron

Here is a large number of hidden features discussed for variety of languages. Now I am curious about some hidden features of XAML and WPF?

这是针对各种语言讨论的大量隐藏功能。现在我对 XAML 和 WPF 的一些隐藏功能感到好奇?

One I have found is the header click event of a ListView

我发现的一个是 ListView 的标题单击事件

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

The GridViewColumnHeader.Click property is not listed.

未列出 GridViewColumnHeader.Click 属性。

Some of relevant features so far:

到目前为止的一些相关功能:

See also:

也可以看看:

  1. Hidden features of C#
  2. Hidden features of Python
  3. Hidden features of ASP.NET
  4. Hidden features of Perl
  5. Hidden features of Java
  6. Hidden features of VB.NET
  7. Hidden features of PHP
  8. Hidden features of Ruby
  9. Hidden features of C
  10. And So On........
  1. C#的隐藏特性
  2. Python 的隐藏特性
  3. ASP.NET 的隐藏功能
  4. Perl 的隐藏特性
  5. Java 的隐藏特性
  6. VB.NET 的隐藏特性
  7. PHP 的隐藏功能
  8. Ruby 的隐藏特性
  9. C 的隐藏特性
  10. 等等........

回答by Julien Poulin

Multibinding(combined with StringFormat):

多重绑定(与 StringFormat 结合):

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

回答by idursun

There is also PresentationTraceSources.TraceLevel trick to debug what is going on with bindings in any particular scenario. All you have to do is to reference System.Diagnostics namespace in WindowsBase assembly

还有 PresentationTraceSources.TraceLevel 技巧可以调试任何特定场景中绑定的情况。您所要做的就是在 WindowsBase 程序集中引用 System.Diagnostics 命名空间

xmlns:sd="clr-namespace:System.Diagnostics;assembly=WindowsBase"

and then add following to the binding expression:

然后将以下内容添加到绑定表达式中:

<TextBlock Text="{Binding Message, sd:PresentationTraceSources.TraceLevel=High}"  />

Log will be like this:

日志将是这样的:

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 

回答by Bryan Anderson

3.5sp1 introduced TargetNullValue to bindings. This will set the bound property to Null if the value is entered and if your property is Null it will display this value.

3.5sp1 将 TargetNullValue 引入到绑定中。如果输入了值,这会将绑定属性设置为 Null,如果您的属性为 Null,它将显示此值。

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

回答by Bryan Anderson

3.5sp1 introduced StringFormat into binding expressions, e.g.

3.5sp1 将 StringFormat 引入绑定表达式,例如

<TextBlock 
  Name="sampleTextBlock" 
  TextTrimming="WordEllipsis" 
  TextWrapping="NoWrap"/>

回答by Prashant Cholachagudda

Sometimes you get string that is too long to show on label. In this case we can make use of TextTrimmingproperty of TextBlockto show Ellipses

有时您会得到太长而无法在标签上显示的字符串。在这种情况下,我们可以利用TextTrimming属性TextBlock来显示椭圆

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

MSDN Link

MSDN链接

回答by Sauron

Adding Aero effect to Window

向窗口添加 Aero 效果

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

回答by Sauron

Generics in XAML with x:TypeArguments

带有 x:TypeArguments 的 XAML 中的泛型

If you want to use an ObservableCollection in XAML you need to create a type that derives from ObservableCollection because you cannot declare it in XAML. With XAML 2009 you can use the x:TypeArguments attribute to define the type of a generic type.

如果你想在 XAML 中使用 ObservableCollection,你需要创建一个派生自 ObservableCollection 的类型,因为你不能在 XAML 中声明它。在 XAML 2009 中,您可以使用 x:TypeArguments 属性来定义泛型类型的类型。

<!-- XAML 2006 -->
<DateTime>00:00:00.0000100</DateTime>

<!-- XAML 2009 -->
<DateTime>
    <x:Arguments>
        <x:Int64>100</x:Int64>
    </x:Arguments>
</DateTime>

回答by Sauron

Use of Non-Default Constructors with x:Arguments

将非默认构造函数与 x:Arguments 结合使用

In XAML 2006 objects must have a public default constructor to use them. In XAML 2009 you can pass constructor arguments by using the x:Arguments syntax.

在 XAML 2006 中,对象必须具有公共默认构造函数才能使用它们。在 XAML 2009 中,您可以使用 x:Arguments 语法传递构造函数参数。

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

回答by Sauron

Show Tooltip on a disabled control

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

Wpf allows to show tooltip on a control, if it is in disabled state.

如果控件处于禁用状态,Wpf 允许在控件上显示工具提示。

For example

例如

<!-- 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}"/>

回答by Thomas Levesque

Markup extensions and attached properties are my favorite features, they enable you to extend XAML "vocabulary" in a very elegant way.

标记扩展和附加属性是我最喜欢的功能,它们使您能够以非常优雅的方式扩展 XAML“词汇表”。

Markup extensions

标记扩展

<!-- 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">

...

Attached properties

附加属性

##代码##

Source for GridViewSort(btw, it uses the GridViewColumnHeader.Clickevent mentioned by Ortus)

GridViewSort 的来源(顺便说一句,它使用了GridViewColumnHeader.ClickOrtus 提到的事件)