I'm doing a Proof of Concept with a Windows Store Application, using WPF/XAML and C# and using the AdventureWorks2014 Sample Database.
Right now I have a Hub with two sections: one with a ListView (shows me the persons) and one with a sort of a form to see how to manipulate data.
This is my XAML:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ACT_POC"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ACT_Service="using:ACT_POC.ACT_Service"
x:Class="ACT_POC.MainPage"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Hub Header="ACT">
<HubSection x:Name="ACT" Header="Persons" HorizontalAlignment="Left" Width="306">
<HubSection.DataContext>
<ACT_Service:WCFServiceClient/>
</HubSection.DataContext>
<DataTemplate>
<ListView x:Name="lv_data" Margin="0" Loaded="lv_data_Loaded" SelectionChanged="lv_data_SelectionChanged" BorderThickness="0" FontSize="14" Padding="0" Width="300" BorderBrush="White" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Left" />
</DataTemplate>
</HubSection>
<HubSection x:Name="PersonalDataHub" Visibility="Collapsed" Width="Auto" HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" Background="#FF00787E" Padding="40,0" Header="Person Data">
<DataTemplate>
<StackPanel x:Name="PersonalDataStack" HorizontalAlignment="Left" Width="Auto" DataContextChanged="PersonalDataStack_DataContextChanged">
<Button x:Name="EditBtn" Width="100" Height="50" VerticalAlignment="Top" HorizontalAlignment="Right" Content="Edit" Background="#FF013263" FontSize="20" Margin="0,0,0,20"/>
<TextBlock Width="Auto" Text="First Name:" HorizontalAlignment="Left" FontSize="14" Margin="0,0,0,5"/>
<TextBox x:Name="txtFirstName" Width="300" Margin="0,0,0,10" FontSize="12"/>
<TextBlock Width="Auto" Text="Last Name:" HorizontalAlignment="Left" FontSize="14" Margin="0,0,0,5"/>
<TextBox x:Name="txtLastName" Width="300" Margin="0,0,0,10" FontSize="12"/>
</StackPanel>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
I'm using a WCF Service to provide data to the application.
Here's my code-behind:
private async void lv_data_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lv_data = (ListView)sender;
ACT_Service.WCFServiceClient client = new ACT_Service.WCFServiceClient();
PersonalDataHub.Visibility = Visibility.Visible;
try
{
await client.OpenAsync();
person = client.GetPersonByIdAsync(((ACT_POC.ACT_Service.PInfo)lv_data.Items[lv_data.SelectedIndex]).personID);
await client.CloseAsync();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void PersonalDataStack_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
StackPanel item = (StackPanel)sender;
TextBox txt = (TextBox)item.FindName("txtFirstName");
txt.Text = person.Result.FirstName.ToString();
}
Now, whenever I change the selectedItem in the ListView I want to be able to update the data on the PersonalDataStack textBox items. So far, I've only managed to get the controls to update by capturing the StackPanel Loaded event or DataContextChanged because I have direct access to the sender. But how can I achieve the intended behaviour if by changing the selectedItem on the ListView the StackPanel's DataContextChanged event isn't raised?
Isn't there any other way I can simplify the process of gaining access to controls directly?