1. 程式人生 > 其它 >WPF資料繫結

WPF資料繫結

1.<ListBox x:Name="listName" Width="248" Height="60">
<ListBoxItem Content="張三"/>
<ListBoxItem Content="李四"/>
<ListBoxItem Content="王五"/>
<ListBoxItem Content="趙柳"/>
</ListBox>
<TextBlock Width="248" Height="24" Text="你所選中的人員名稱:" />
<TextBlock Width="248" Height="24" Text="{Binding ElementName=listName, Path=SelectedItem.Content}">

2.<DataGrid x:Name="dsGrid" AutoGenerateColumns="False" ItemsSource="{Binding Students}" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Width="100" Header="名稱" />
<DataGridTextColumn Binding="{Binding Age}" Width="80" Header="年齡" />
<DataGridTextColumn Binding="{Binding Country}" Width="150" Header="國家" />
</DataGrid.Columns>
</DataGrid>

public class Student
{

public string Name { get; set ;}

public string Age { get; set ; }
public string Country { get; set; }

}

public List<Student> GetStudentList()

{

Student liang = new Student();

liang.Age = "1";

liang.Name = "丘";

liang.Country = "中國";

}

this.DataContext = this;

List<Student> students = GetStudentList();

dsGrid.ItemsSource = students;