Search code examples
c#wpfsilverlightdatasourcedomainservices

Refresh Grid with new DataContext WPF


I have a grid with a domain service binded to the data context. I would like to refresh that data context.

I have the code:

            <Grid DataContext="{Binding Data, ElementName=userDomainDataSource}" Name="gridAccountDetails">
                <sdk:Label Content="Address Line" HorizontalAlignment="Left" Margin="443,67,0,0" d:LayoutOverrides="VerticalAlignment" VerticalAlignment="Top" />
                <TextBox HorizontalAlignment="Left" Margin="537,59,0,0" x:Name="txtAccountDetailsAddress" Text="{Binding addressline, Mode=OneWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" d:LayoutOverrides="VerticalAlignment" Height="24" VerticalAlignment="Top" DataContext="{Binding}" />
                <sdk:Label Content="County" HorizontalAlignment="Left" Margin="443,142,0,143" d:LayoutOverrides="Height" />
                <TextBox HorizontalAlignment="Left" Margin="537,134,0,143" x:Name="txtAccountDetailsCounty" Text="{Binding county, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" />
                <sdk:Label Content="Email" HorizontalAlignment="Left" Margin="26,143,0,142" d:LayoutOverrides="HorizontalAlignment, Height" />
                <TextBox HorizontalAlignment="Left" Margin="114,136,0,142" x:Name="txtAccountDetailsEmail" Text="{Binding email, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" d:LayoutOverrides="HorizontalAlignment" />

                <sdk:Label Content="First Name" HorizontalAlignment="Left" Margin="26,67,0,0" VerticalAlignment="Top" />
                <TextBox HorizontalAlignment="Left" Margin="114,60,0,0" x:Name="txtAccountDetailsFirstname" Text="{Binding firstname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Width="233" Height="23" VerticalAlignment="Top" d:LayoutOverrides="HorizontalAlignment" />

                <sdk:Label Content="Last Name" HorizontalAlignment="Left" Margin="26,104,0,0" VerticalAlignment="Top" />
                <TextBox HorizontalAlignment="Left" Margin="114,97,0,0" x:Name="txtAccountDetailsLastname" Text="{Binding lastname, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="23" VerticalAlignment="Top" d:LayoutOverrides="HorizontalAlignment" />
                <sdk:Label Content="Postcode" HorizontalAlignment="Left" Margin="443,0,0,104" VerticalAlignment="Bottom" />
                <TextBox HorizontalAlignment="Left" Margin="537,0,0,104" x:Name="txtAccountDetailsPostcode" Text="{Binding postcode, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="24" VerticalAlignment="Bottom" />
                <sdk:Label Content="Town" Margin="0,105,323,0" VerticalAlignment="Top" HorizontalAlignment="Right" />
                <TextBox HorizontalAlignment="Left" Margin="537,0,0,180" x:Name="txtAccountDetailsTown" Text="{Binding town, Mode=TwoWay, NotifyOnValidationError=true, TargetNullValue='', ValidatesOnExceptions=true}" Width="233" Height="24" VerticalAlignment="Bottom" />
                <Button Content="Edit Account Details" HorizontalAlignment="Right" Margin="0,0,26,35" VerticalAlignment="Bottom" Width="136"/>
            </Grid>

I have set the domain source with the code:

EntityQuery<Web.User> qry =
                    from u in usrClass.getQuery()
                    where u.userId == usrClass.getUserId()
                    select u;
                LoadOperation<Web.User> loadU = usrClass.loadUsersQuery(qry);
                userDomainDataSource.DataContext = loadU.Entities;

I have tried using:

gridAccountDetails.DataContext = userDomainDataSource.DataContext;

and

gridAccountDetails.DataContext = userDomainDataSource.Data;

with no luck

But I would like to refresh the datasource so the new values get put into the textboxes, as these can be changed by the user.

Can anyone provide any information. Thanks


Solution

  • Well you have to tell "gridAccountDetails" that the "Data" of "userDomainDataSource" changed. Most widely used for this is to raise a PropertyChanged event altough I'm not sure how this would fit with your service, possibly consider a buffer from the service to your Grid.

    One more thing once binded I wouldn't touch the datacontext of your Grid, you're binding it and then overwriting.