WPF绑定方式

1.

stu = new Student();
Binding binding = new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name");

BindingOperations.SetBinding(this.textBoxName, TextBox.TextProperty, binding); 

或者 

this.textBoxName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu = new Student() });

 

public class Student : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
                }
            }

        } 

 

2.把控件作为Binding源、Binding标记拓展

 

<TextBox x:Name="textBox1" Text="{Binding Value,ElementName=slider1}" BorderBrush="Black" Margin="5"></TextBox>
<Slider x:Name="slider1" Maximum="100" Minimum="0" Margin="5"></Slider>

 

3. this.listBoxStuents.ItemsSource = stuList;//List<T>集合

 <StackPanel x:Name="stackPanel" Background="LightBlue">

        <TextBlock Text="Student ID:" FontWeight="Bold" Margin="5"></TextBlock>
        <TextBox x:Name="textBoxId" Margin="5"></TextBox>
        <TextBlock Text="Student List:" FontWeight="Bold" Margin="5"></TextBlock>
        <ListBox x:Name="listBoxStuents" Height="110" Margin="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Id}" Width="30"></TextBlock>
                        <TextBlock Text="{Binding Name}" Width="60"></TextBlock>
                        <TextBlock Text="{Binding Age}" Width="30"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

 

posted on 2012-03-20 23:01  上校  阅读(546)  评论(0编辑  收藏  举报