wpf StaticResource x:static ElementName 区别

StaticResource指向的对象通常是Xaml里定义的Style或者Template之类。
x:static指向的对象通常是后台代码里的某个静态类。用来在Xaml中引用类的静态字段或静态属性,如Text={x:Static local:Window1.ShowText}。

 

原文出处:http://www.im47.cn/?p=482

ElementName
The ElementName property is used to reference an object by the name of the object. This is particularly useful in XAML, where you can directly reference other elements defined in XAML.

举例

<StackPanel Background=”Blue”>
<Button x:Name=”refButton” Background=”Orange”/>
<Button Background=”{Binding ElementName=refButton, Path=Background}”/>
</StackPanel>

Source
The Source property is used to specify an object reference on which the binding Path or XPath will be evaluated. The Source property is usually used when the object on which the Binding is set is known and differs from the DataContext.

举例:

<CustomClass1 Property1=”{Binding Source={x:Static DateTime.Now}, Path=Day}”/>

<CustomClass1 Property1=”{Binding Source={StaticResource AnotherElement}, Path=ActualWidth}”/>

 

简单地说,“ElementName”用于绑定到xaml界面设计中添加的其它控件对象,“Source”用于绑到资源中的对象。
————————————————
版权声明:本文为CSDN博主「Metal1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Metal1/article/details/41944697

 

 

X:Static是一个常用的标记扩展,它的功能是在XAML文档中使用数据类型的static成员。因为XAML中是不能编写逻辑代码的,所以使用x:Static访问static成员一定是数据类型的属性或字段。

下面是实例:
首先在Window里面添加两个成员,一个是static字段,一个是static属性。

public partial class Windows : Window
{
    public static string Titles = "Hello";
    public static string text { get { return "World"; } }
    public Windows()
    {
       InitializeComponent();
    }
}

 

然后在XAML中使用x:Static来访问这两个成员:

<Window x:Class="WpfApplication1.Key"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="{x:Static local:Key.Titles}" Height="300" Width="300">
    <StackPanel>
        <TextBlock FontSize="20" Text="{x:Static local:Key.text}"/>
    </StackPanel>
</Window>

————————————————
版权声明:本文为CSDN博主「 认」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44552202/article/details/93967376

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunnf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值