WPF和XAML的隐藏功能?

Sau*_*ron 123 wpf xaml hidden-features

以下是针对各种语言讨论的大量隐藏功能.现在我好奇XAML和WPF的一些隐藏功能?

我发现的是ListView的标题点击事件

<ListView x:Name='lv' 
      Height="150" 
      GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
Run Code Online (Sandbox Code Playgroud)

未列出GridViewColumnHeader.Click属性.

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

也可以看看:

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

Jul*_*lin 87

多绑定(与StringFormat结合):

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat="{}{0}, {1}">
      <Binding Path="LastName" />
      <Binding Path="FirstName" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

  • 这很棒,但我很想不去做.如果我需要构建一个字符串,我会将其归类为逻辑,并希望对输出进行单元测试.像这样的东西有时候在视图模型中更好地作为string.Format(). (5认同)
  • 大声笑我只是希望我知道这个loooong = P之前 (3认同)

idu*_*sun 58

还有PresentationTraceSources.TraceLevel技巧来调试任何特定场景中绑定的内容.您所要做的就是在WindowsBase程序集中引用System.Diagnostics名称空间

xmlns:sd="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Run Code Online (Sandbox Code Playgroud)

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

<TextBlock Text="{Binding Message, sd:PresentationTraceSources.TraceLevel=High}"  />
Run Code Online (Sandbox Code Playgroud)

日志将是这样的:

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 
Run Code Online (Sandbox Code Playgroud)

  • 在VisualStudio 2010中,您需要将跟踪设置的级别设置为警告!见http://stackoverflow.com/questions/2802662/any-reason-why-presentationtracesources-tracelevelhigh-would-not-print-any-info/3004469#3004469 (4认同)

Bry*_*son 44

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

<TextBox Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}" />
Run Code Online (Sandbox Code Playgroud)

  • 在StringFormat周围放置单引号应删除一些编译器警告 - "Text = {Binding Date,StringFormat ='{} {0:MM/dd/yyyy}'}"` (6认同)

Bry*_*son 44

3.5sp1将TargetNullValue引入绑定.如果输入值,这将绑定属性设置为Null,如果属性为Null,它将显示此值.

<TextBox Text="{Binding Total, TargetNullValue=$0.00}" />
Run Code Online (Sandbox Code Playgroud)


Pra*_*dda 29

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

<TextBlock 
  Name="sampleTextBlock" 
  TextTrimming="WordEllipsis" 
  TextWrapping="NoWrap"/>
Run Code Online (Sandbox Code Playgroud)

MSDN链接


Sau*_*ron 27

将Aero效果添加到Window

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


Sau*_*ron 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 />
Run Code Online (Sandbox Code Playgroud)


Sau*_*ron 19

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

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

例如

<Button Content="Disabled Button" ToolTipService.ShowOnDisabled="True" IsEnabled="False" ToolTip="This is a disabled button"/> 
Run Code Online (Sandbox Code Playgroud)


Sau*_*ron 19

使用带有x:参数的非默认构造函数

在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>
Run Code Online (Sandbox Code Playgroud)


Bry*_*son 18

不是真正隐藏的功能,但使用WPF/XAML,你会得到Bea StollnitzJosh Smith.WPF/XAML编程的女王和王者.

  • 什么是卡尔?千斤顶?还是小丑? (3认同)

Tho*_*que 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}"/>
Run Code Online (Sandbox Code Playgroud)

附属物

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

...
Run Code Online (Sandbox Code Playgroud)

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


M. *_*ley 15

您可以使用加号(+)在XAML中引用嵌套类型.例如,如果我们有这个类:

public class SomeClass
{
    public enum SomeEnum
    {
        SomeValue
    };
}
Run Code Online (Sandbox Code Playgroud)

我们可以SomeValue使用以下语法在XAML中引用:

{x:Static local:SomeClass+SomeEnum.SomeValue}
Run Code Online (Sandbox Code Playgroud)

MSDN上未记录此语法,并且不受官方支持.有人在MSDN论坛上询问过这个问题,显然它打破了VS2010的WPF Designer.它已被报道微软连接.


Bry*_*son 14

网格大小共享(这是一个很好的例子).简而言之,即使跨越不同的网格,您也可以将网格列和行共享大小.对于那些使用DataGrids而不需要编辑数据的人来说,这将是非常宝贵的.


Ser*_*hov 11

PriorityBinding.允许您以"先到先得"的顺序使用asyn绑定:

<TextBlock.Text>
      <PriorityBinding FallbackValue="defaultvalue">
        <Binding Path="SlowestDP" IsAsync="True"/>
        <Binding Path="SlowerDP" IsAsync="True"/>
        <Binding Path="FastDP" />
      </PriorityBinding>
</TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)


Sau*_*ron 10

静态工厂方法与x:FactoryMethod的使用

当您的类型没有公共构造函数但是静态工厂方法时,您必须在XAML 2006中的代码中创建该类型.使用XAML 2009,您可以使用x:FactoryMethodx:Arguments属性来传递参数值.

<!-- XAML 2006 -->
Guid id = Guid.NewGuid();

<!-- XAML 2009 -->
<Guid x:FactoryMethod="Guid.NewGuid" />
Run Code Online (Sandbox Code Playgroud)


awe*_*awe 7

高级"标题"属性

另一件不太清楚的事情是我们习惯只包含文本的一些属性的内容.如果GUI元素的属性是Object类型,则很可能您可以添加包含一组控件的需要的面板,而不仅仅是设置文本.

一个例子是MenuItem,其中Header属性(通常只包含文本)可以包含一组包含在面板控件中的gui元素(如果只需要一个,则只包含一个gui元素).

另请注意IconMenuItem上的属性.这通常包含一个Image元素,但这也可以包含任何东西!

<MenuItem Name="MyMenuItem" Click="MyMenuItem_Click">
  <MenuItem.Icon>
    <Button Click="Button1_Click">i</Button>
  </MenuItem.Icon>
  <MenuItem.Header>
     <StackPanel Orientation="Horizontal" >
        <Label>My text</Label>
        <Button Click="Button2_Click">ClickMe!</Button>
     </StackPanel>
  </MenuItem.Header>
</MenuItem>
Run Code Online (Sandbox Code Playgroud)


Sau*_*ron 6

内置类型

如果要在今天的资源字典中添加简单类型的对象(如string或double),则需要将所需的clr-namespace映射到XML名称空间.在XAML 2009中,我们提供了许多XAML语言中包含的简单类型.

<!-- XAML 2006 -->
<sys:String xmlns:sys="clr-namespace:System;assembly=mscorlib >Test</sys:String>

<!-- XAML 2009 -->
<x:String>Test</x:String>
Run Code Online (Sandbox Code Playgroud)

XAML语言包含以下类型:

<x:Object/> 
<x:Boolean/> 
<x:Char/> 
<x:String/> 
<x:Decimal/> 
<x:Single/> 
<x:Double/> 
<x:Int16/> 
<x:Int32/> 
<x:Int64/> 
<x:TimeSpan/> 
<x:Uri/> 
<x:Byte/> 
<x:Array/> 
<x:List/> 
<x:Dictionary/> 
Run Code Online (Sandbox Code Playgroud)


Sau*_*ron 6

使用{x:Reference}轻松引用对象

如果要在今天创建对象引用,则需要进行数据绑定并使用ElementName声明源.在XAML 2009中,您可以使用新的{x:Reference}标记扩展

<!-- XAML 2006 -->
<Label Target="{Binding ElementName=firstName}">FirstName</Label>
<TextBox x:Name="firstName" />

<!-- XAML 2009 -->
<Label Target="{x:Reference firstName}">FirstName</Label>
<TextBox x:Name="firstName" />
Run Code Online (Sandbox Code Playgroud)

  • Target在WPF 4.0中只接受`Target ="firstName"`.:) (9认同)

See*_*arp 6

系统颜色用法

<Border Background="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}"/>
Run Code Online (Sandbox Code Playgroud)

  • 将其指定为DynamicResource非常重要,因为用户可以在应用程序运行时更改系统颜色. (3认同)