WEB开发网
开发学院WEB开发ASP.NET WPF学习笔记8. Binding 阅读

WPF学习笔记8. Binding

 2010-10-11 16:21:09 来源:本站整理   
核心提示:(2) 绑定到静态资源<Windowx:Class="Learn.WPF.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schema

(2) 绑定到静态资源

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1">
  <Window.Resources>
    <ContentControl x:Key="text">Hello, World!</ContentControl>
  </Window.Resources>
  <Grid>
    <StackPanel>
      <Label x:Name="label1" Content="{Binding Source={StaticResource text}}" />
    </StackPanel>
  </Grid>
</Window>

System.Windows.Data.Binding.Source 并不是一个依赖属性,因此我们无法将其绑定到一个动态资源(DynamicResouce)上。

(3) 绑定到自身

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1">
  <Grid>
    <StackPanel>
      <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource Self}, Path=Name}" />
    </StackPanel>
  </Grid>
</Window>

(4) 绑定到指定类型的父元素

<Window x:Class="Learn.WPF.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1">
  <Grid x:Name="Grid1">
    <StackPanel>
      <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,
        AncestorType={x:Type Grid}}, Path=Name}" />
    </StackPanel>
  </Grid>
</Window>

3. 绑定到普通对象

WPF 允许将任何 .NET 对象作为数据绑定源。

class Data
{
  public string Name { get; set; }
}
public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
    var data = new Data { Name = "Q.yuhen" };
    BindingOperations.SetBinding(this.textbox1, TextBox.TextProperty, new Binding("Name") { Source = data });
  }
}

上一页  1 2 3 4  下一页

Tags:WPF 学习 笔记

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接