当Binding源本身就是数据且不需要Path来指明时,可以设置Path的值为".",或直接省略Path。XAML中这个"."可以省略不写,但在C#代码中是不能省略的。

XAML:

<Window x:Class="没有Path的Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="155.129" Width="209.615" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<StackPanel.Resources>
<sys:String x:Key="myString">
菩提本无树,明镜亦非台。
本来无一物,何来惹尘埃。
</sys:String>
</StackPanel.Resources>
<TextBlock x:Name="textBlock1" TextWrapping="Wrap"
Text="{Binding Path=.,Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5" Background="Yellow"></TextBlock>
<!--标准的写法是上面那个,下面这两个也是一样的-->
<!--<TextBlock x:Name="textBlock1" TextWrapping="Wrap"
Text="{Binding .,Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5" Background="Yellow"></TextBlock>-->
<!--<TextBlock x:Name="textBlock1" TextWrapping="Wrap"
Text="{Binding Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5" Background="Yellow"></TextBlock>-->
<TextBlock x:Name="textBlock2" FontSize="16" Margin="5" TextWrapping="Wrap" Background="Green"></TextBlock>
</StackPanel>
</Grid>
</Window>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace 没有Path的Binding
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
string str = "菩提本无树,明镜亦非台。本来无一物,何来惹尘埃。";
this.textBlock2.SetBinding(TextBlock.TextProperty, new Binding(".") { Source = str });
}
}
}

截图:

没有Path的Binding的更多相关文章

  1. Binding的Path(路径)

    Binding的源可以是控件(一个控件是另一个控件的Source.控件把自己的容器作为Source),把集合作为ItemsControls的Source,把xml作为Tree或者Menu的Source ...

  2. Binding笔记

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...

  3. WPF之Binding初探

    初学wpf,经常被Binding搞晕,以下记录写Binding的基础. 首先,盗用张图.这图形象的说明了Binding的机理. 对于Binding,意思是数据绑定,基本用法是: 1.在xmal中使用 ...

  4. Binding

    Binding基础  绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event  ...

  5. WPF之Binding深入探讨

    原文:http://blog.csdn.net/fwj380891124/article/details/8107646 1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在 ...

  6. WPF在XAML中Binding使用StringFormat属性

    1. 绑定Currency, 如果没有字符的话, =后面需要先加入{}. 不加的话会出问题. 1 <TextBlock Text="{Binding Amount, StringFor ...

  7. WPF的Binding功能解析

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  8. Binding的Source从何而来?

    I. Binding to Object 1. Binding data using ObjectDataProvider AC:Let’s say there is a CLR based data ...

  9. Validation Rule和Binding Group

    WPF学习之绑定-Validation Rule和Binding Group 在上一篇文章中我们讨论了有关WPF绑定的知识点,现在我们可以很容易的将业务数据作为源绑定到WPF控件并可以通过创建不同的D ...

随机推荐

  1. Android 常用工具类之 ScreenUtil

    需求: 截屏 参考 :    Android开发:截屏 screenshot 功能小结 package bvb.de.openadbwireless.utils; import android.app ...

  2. Java常见编码方式简介

    计算机只能识别二进制数据,早期由来是电信号. 为了方便应用计算机,让它可以识别各个国家的文字. 就将各个国家的文字用数字来表示,并一一对应,形成一张表. ASCII:美国标准信息交换码. 用一个字节的 ...

  3. jQuery.isNumeric() 和 js isNaN()

    jQuery.isNumeric( value ) Description: 判断指定参数是否是一个数字值(字符串形式的数字也符合条件),返回 true 或者 false. Example: $.is ...

  4. JS获取非行间样式

    我们都知道用offset函数获取元素样式是一件很方便的事,但是offset只能获取行间样式,而无法获得非行间样式,这是它的瓶颈所在. 我们都知道js获取行间样式的方法,那么js是如何获取行距样式的呢? ...

  5. SlickGrid example 4: 过滤

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. c# UDP

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. hibernate有关联关系删除子表时可能会报错,可以用个clear避免错误

    //清除子表数据 public SalesSet removeSalesSetDistributor(SalesSet salesSet ){ List<SalesSetDistributor& ...

  8. WPF中父子窗口的层次关系

    关于子窗体的层级关系总结一下哈,希望能对大家有些帮助 假设有这样两个窗体:RootWindow,SubWindow,在RootWindow中引发某事件而显示SubWindow 1,如果弹出窗体(比如S ...

  9. Easyui Layout Center 全屏方法扩展

    $.extend($.fn.layout.methods, { full : function (jq) { return jq.each(function () { var layout = $(t ...

  10. (2)redis的基本数据结构是动态数组

    redis的基本数据结构是动态数组 一.c语言动态数组 先看下一般的动态数组结构 struct MyData { int nLen; ]; }; 这是个广泛使用的常见技巧,常用来构成缓冲区.比起指针, ...