0

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?

Zed_Blade
  • 1,009
  • 3
  • 18
  • 38
  • 2
    You don't need to access controls. Developing XAML-based applications using WinForms approach is not a way to go. You definitely must read about MVVM. – Dennis Nov 26 '14 at 12:30
  • Maybe I'm getting it wrong, but since I'm providing Entity Framework Entities as a replly on the WCF Service, shouldn't I be able to not use a model but use these classes/entities as models? – Zed_Blade Nov 26 '14 at 12:54
  • Model -> property in ViewModel -> binding -> window controls. – Sinatr Nov 26 '14 at 12:56
  • @Zed_Blade: MVVM isn't about models/view models only. XAML-based frameworks are highly dependent on data binding. Data templates are one of the reasons, why data context/data binding is preferable and painless approach to perform data exchange between models/view models and framework elements. There are no any controls, until data template is applied to some item. And even after that moment, correct traverse of visual tree is a rather complex task. That's why you should at least use data binding. – Dennis Nov 26 '14 at 13:00
  • While this is an English language website, it is used by developers from all over the world. Please do not use abbreviations such as POC, as many users will have no idea what they stand for. For example, I would guess that you meant 'Proof Of Concept', although searching online would also return 'People Of Colour'. – Sheridan Nov 26 '14 at 13:13
  • I accept that the 'duplicate' question is not an exact duplicate, but the accepted answer is applicable to all collection controls including the `ListView` and will show you how to do what you want. You can also find another example in my answer to the [WPF selected value in `ComboBox`](http://stackoverflow.com/questions/21944377/wpf-selected-value-in-combobox/21945504#21945504) question. – Sheridan Nov 26 '14 at 13:24
  • So, as far as I understand it, I've got it all wrong.. I should define Models based on EF on a separate project that can then be used in my WCF Service and also referenced by my client application is that it? – Zed_Blade Nov 26 '14 at 15:39

0 Answers0