子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

WPF 数据绑定 一

控制项和控制项做Binding 

Image图片的高度随着Slider的滑动而改变,注意Binding ElementName=slider1,Path=Value。这句中,ElementName是Slider的Name属性,Path是指Slider的值属性Value,V要大写,C#区分大小写。

 <Image  
                Source="D:\temp\j.png" 
                Height="{Binding ElementName=slider1,Path=Value }" 
                Margin="28,0,0,60" Name="image1" Stretch="Fill" VerticalAlignment="Bottom" 
                HorizontalAlignment="Left" Width="61" />
        <Slider Height="22" Margin="206,0,138,60" Name="slider1" VerticalAlignment="Bottom" Maximum="100" />

Resource 绑定

Window1.xaml.cs文件

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;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;//INotifyPropertyChanged需要引进该空间

namespace WPF_BindingTest
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>

public partial class Window1 : Window, INotifyPropertyChanged
{
private string name;
public event PropertyChangedEventHandler PropertyChanged;

public Window1(string value)
{
this.name = value; }
public string Name
{
get { return name; }
set
{
name
= value;
OnPropertyChanged(
"Name");
}

}

public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(
this, new PropertyChangedEventArgs(name));
}
}
public Window1()
{
InitializeComponent();


}
}

Window1.xaml  主要代码

<Window x:Class="WPF_BindingTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:src="clr-namespace:WPF_BindingTest"       // src是一个标识符,以便下面内容引用该命名空间里内容时作为前缀使用,上一行的system同理。
       
    Title="Window1" Height="300" Width="467">

<Grid.Resources>
            <ObjectDataProvider x:Key="myDataSource" ObjectType="{x:Type src:Window1}">
                <ObjectDataProvider.ConstructorParameters>
                    <system:String>台湾讲师</system:String>
                </ObjectDataProvider.ConstructorParameters>
            </ObjectDataProvider>
        </Grid.Resources>

<TextBox Height="23" Margin="175,0,151,99" Name="textBox1" VerticalAlignment="Bottom" >
            <TextBox.Text>
                <Binding Source="{StaticResource myDataSource}" Path="Name">
                </Binding>
            </TextBox.Text>
        </TextBox>
        <TextBox Height="23" Margin="175,0,150,60" Name="textBox2" VerticalAlignment="Bottom" >
            <TextBox.Text>
                <Binding Source="{StaticResource myDataSource}" Path="Name" UpdateSourceTrigger="PropertyChanged">
                </Binding>
            </TextBox.Text>
        </TextBox>

鼠标在 第二个文本框 里写东西第一个会显示一样的,第一个写第二个没反应。
posted on 2011-03-01 20:31  人的本质是什么?  阅读(302)  评论(0)    收藏  举报