首页 > 其他 > 详细

没有Path的Binding

时间:2014-01-25 16:28:47      阅读:460      评论:0      收藏:0      [点我收藏+]

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

 XAML:

bubuko.com,布布扣
<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>
bubuko.com,布布扣

 

 

C#:

bubuko.com,布布扣
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 });
        }
    }
}
bubuko.com,布布扣

 

截图:

bubuko.com,布布扣

没有Path的Binding

原文:http://www.cnblogs.com/KeenLeung/p/3533014.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!