This is a really strange issue, this is the last page that i was working on, i used to make an ObservableCollection of the ListBox to add the data in the textblocks and image and bind the data.
But this time i only have 1 TextBlock and 1 Image that i need to bind the Data to it. In the .cs file, i can't access them directly and also DataBinding is not working.
Xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="{Binding lastName}" Height="33" Margin="0,175,8,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Right" Width="336" />
<ListBox ItemsSource="{Binding lastAddress}" Margin="8,291,8,8"/>
<TextBlock HorizontalAlignment="Left" Height="33" Margin="8,0,0,0" TextWrapping="Wrap" Text="Restaurant Profile: " VerticalAlignment="Top" Width="225" TextDecorations="Underline" Foreground="#FF7A0100" FontSize="24"/>
<Image Source="{Binding lastImage}" Height="132" Margin="8,37,292,0" VerticalAlignment="Top" Stretch="None"/>
</Grid>
Any help ? Thanks.
Looks like you have a couple of potential problems here. First of all, if you want to access your controls from your codebehind .cs
file, you need to give the controls names, like this:
<Grid Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Name="MyTextBlock" />
</Grid>
Then they are accessible in your codebehind:
public MainPage()
{
InitializeComponent();
DoStuff();
}
private void DoStuff()
{
MyTextBlock.Text = "Hey, it works!";
}
Second, in order to get the databinding working, you need to set the DataContext
for your page, either in the XAML or in your codebehind file.
In the codebehind file, you can do it like this:
public MainPage()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}
Which allows you to databind like this:
<Grid Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Name="MyTextBlock" Text="{Binding MyProperty}"/>
</Grid>