I have a class Student with 3 attributes (first name, last name, age).
In .xaml ( basically two rectangles – each containing 3 text blocks bind to those 3 attributes).
<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>
In .xaml.cs
Student student1 = new Student { FirstName = "James", LastName = "Peter", Age= 12 ,IsAvailable=true };
Student student2 = new Student { FirstName = "Mark", LastName = "Smith", Age = 20 };
after InitializeComponent
InitializeComponent();
DataContext = student1;
When i run i get
James Peter 12
James Peter 12
I want on form load
James Peter 12
Mark Smith 20
I tried to use this, but didn’t work:
rectangle1.DataContext=student1;
rectangle2.DataContext=student2;
How do i set two different values for these 2 rectangles?
The
Rectanglesdo not contain theTextBlocks, in both cases aStackPaneldoes, you should set theDataContexton them instead.