8,735
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Loaded="page_loaded"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel x:Name="panel_1" Grid.Row="0" Height="auto">
<ComboBox x:Name="comboBox" Grid.Row="0" ItemsSource="{Binding StudyList}"
DisplayMemberPath="Name" SelectedValuePath="Name" SelectedItem="{Binding SelectedStudy,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button1" Content="动态增加textbox" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Top" Width="75" Click="button_Click" />
</StackPanel>
<StackPanel x:Name="panel_2" Grid.Row="1">
<TextBlock>下面是2个TextBox</TextBlock>
<TextBox Text="{Binding SelectedStudy.Name}" ></TextBox>
</StackPanel>
</Grid>
</Window>
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
private ObservableCollection<Study> studyList= new ObservableCollection<Study>();
public ObservableCollection<Study> StudyList
{
get
{
return studyList;
}
set
{
studyList = value;
}
}
private Study selectedStudy = new Study();
public Study SelectedStudy
{
get
{
return selectedStudy;
}
set
{
selectedStudy = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SelectedStudy"));
}
}
}
public MainWindow()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void button_Click(object sender, RoutedEventArgs e)
{
TextBox t = new TextBox();
t.Margin = new Thickness(3);
t.Text = "this is my text";
this.panel_2.Children.Add(t);
this.panel_2.RegisterName("mytext1", t);
t.SetBinding(TextBox.TextProperty, new Binding("Name") {
Source= StudyList,
Mode= BindingMode.TwoWay,
UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged
});
}
private void page_loaded(object sender, RoutedEventArgs e)
{
StudyList.Add(new Study() { Selected = true, Age = 20, Name = "Tom" });
StudyList.Add(new Study() { Selected = true, Age = 22, Name = "Jack" });
StudyList.Add(new Study() { Selected = true, Age = 23, Name = "Jimi" });
StudyList.Add(new Study() { Selected = true, Age = 24, Name = "Cat" });
}
}
}
public class Study: INotifyPropertyChanged
{
private string name;
private int age;
private bool selected;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get
{
return name;
}
set
{
name = value;
if (null != PropertyChanged)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
if (null != PropertyChanged)
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
public bool Selected
{
get
{
return selected;
}
set
{
selected = value;
if (null != PropertyChanged)
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Selected"));
}
}
}
t.SetBinding(TextBox.TextProperty, new Binding("SelectedStudy.Name") {
Source= this,
Mode= BindingMode.TwoWay,
UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged
});
t.SetBinding(TextBox.TextProperty, new Binding("Name") {
Source= SelectedStudy,
Mode= BindingMode.TwoWay,
UpdateSourceTrigger= UpdateSourceTrigger.PropertyChanged
});