0
我有一個具有3個屬性(名,姓,年齡)的Student類。將DataContext綁定到一個矩形
在.xaml(基本上兩個矩形 - 每個包含3個文本塊綁定到這3個屬性)。
<Border Grid.Column="0" Grid.Row="5" >
<StackPanel Orientation="Horizontal">
<Rectangle Width="16" Height="16" Name="rectangle1" />
<TextBlock Text="{Binding Mode=TwoWay, Path=FirstName}" Padding="2"/>
<TextBlock Text="{Binding Mode=TwoWay, Path=LastName}" Padding="2"/>
<TextBlock Text="{Binding Mode=TwoWay, Path=Age}" Padding="2"/>
</StackPanel>
</Border>
<Border Grid.Column="0" Grid.Row="6">
<StackPanel Orientation="Horizontal">
<Rectangle Width="16" Height="16" Fill="{Binding Converter={StaticResource AvailabilityToBrushConverter1}, Path=IsAvailable}" Name="rectangle2"/>
<TextBlock Text="{Binding Mode=TwoWay, Path=FirstName}" Padding="2"/>
<TextBlock Text="{Binding Mode=TwoWay, Path=LastName}" Padding="2"/>
<TextBlock Text="{Binding Mode=TwoWay, Path=Age}" Padding="2"/>
</StackPanel>
</Border>
在.xaml.cs
Student student1 = new Student { FirstName = "James", LastName = "Peter", Age= 12 ,IsAvailable=true };
Student student2 = new Student { FirstName = "Mark", LastName = "Smith", Age = 20 };
後的InitializeComponent
InitializeComponent();
DataContext = student1;
當我運行得到
詹姆斯·彼得12
詹姆斯·彼得12
我想在窗體加載
詹姆斯·彼得12
馬克·史密斯20
我試圖用這一點,但沒有奏效:
rectangle1.DataContext=student1;
rectangle2.DataContext=student2;
怎麼辦我爲這兩個矩形設置了兩個不同的值?