PropertyStatus & Silverlight - what might I be missing?

PropertyStatus & Silverlight - what might I be missing?

Old forum URL: forums.lhotka.net/forums/t/6173.aspx


skagen00 posted on Thursday, January 15, 2009

My Csla Data Source Looks like:

        <csla:CslaDataProvider x:Key="StaffData"
                           ManageObjectLifetime="True"      
                           DataChanged="CslaDataProvider_DataChanged"                 
                           ObjectType="<company>.<application>.Business.Staff, <company>.<application>.Business, Version=..., Culture=neutral, PublicKeyToken=null"
                           FactoryMethod="GetStaff" />

The relevant Xaml is:

<Grid x:Name="LayoutRoot" DataContext="{StaticResource StaffData}">  
  ...<snip>...
 <TextBlock Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right"
  Margin="0,0,3,0" VerticalAlignment="Center" Text="First Name"/>
 <TextBox Grid.Column="1" Grid.Row="1" x:Name="FirstNameTextBox"
  Text="{Binding Mode=TwoWay, Path=FirstName}" BorderThickness="0,0,0,0" Margin="2,3,2,3" 
  VerticalAlignment="Center" MaxLength="25"/>
        <csla:PropertyStatus Grid.Column="2" Grid.Row="1"  RelativeTargetName="FirstNameTextBox" Property="FirstName"
  Source="{Binding Path=Data}" />

...<snip>...
</Grid>

My code behind looks like:

        public void LoadStaffData(string staffId)
        {
            CslaDataProvider provider = this.Resources["StaffData"] as CslaDataProvider;
            provider.FactoryParameters.Clear();
            provider.FactoryParameters.Add(staffId);
            provider.Refresh();
        }

        private void CslaDataProvider_DataChanged(object sender, EventArgs e)
        {
            CslaDataProvider provider = ((CslaDataProvider)this.Resources["StaffData"]);
            LayoutRoot.DataContext = provider.Data;
            if (provider.Error != null)
                System.Windows.Browser.HtmlPage.Window.Alert(((Csla.Silverlight.CslaDataProvider)sender).Error.Message);
        }

Here's what works:

Here's what doesn't work:

Any ideas?

RockfordLhotka replied on Thursday, January 15, 2009

Your binding is kind of messed up.

In your XAML you set the binding of LayoutRoot to the data provider, which isn't quite right - you should set it to the Data property:

DataContext="{Binding Source={StaticResource StaffData}, Path=Data}"

You probably discovered this problem though, because in the data changed event handler you set the DataContext in code to the Data property. That's why your normal controls are working.

But then you set the PropertyStatus.Source property to the Data property of the current DataContext. So really you are binding the PropertyStatus to a non-existant Data property of your business object. You need the binding expression to just be {Binding}.

Also, you don't want to set the DataContext in the data changed event - that's probably going to cause you trouble long into the future. What you really want to do is my first suggestion - set the DataContext of LayoutRoot correctly in the XAML.

skagen00 replied on Thursday, January 15, 2009

I was pretty sure I shouldn't have had to set the DataContext there but setting it was the only thing getting me anywhere :). Just before reading your post I was able to get it to work by having the source on the PropertyStatus to be {Binding}, indeed.

So, I'm good to go now. Works great.

Thanks for taking the time, I'm fresh to WPF/Silverlight databinding and was trying to mimic CompanyEditor.xaml in the Rolodex app.

Chris

 

 

RockfordLhotka replied on Thursday, January 15, 2009

The problem, of course, is that there are numerous ways to set the data context and/or binding expressions – several of which work. The trick is to pick one model and be consistent, because mixing models causes problems.

 

Rocky

 

 

From: skagen00 [mailto:cslanet@lhotka.net]
Sent: Thursday, January 15, 2009 2:45 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] PropertyStatus & Silverlight - what might I be missing?

 

I was pretty sure I shouldn't have had to set the DataContext there but setting it was the only thing getting me anywhere :). Just before reading your post I was able to get it to work by having the source on the PropertyStatus to be {Binding}, indeed.

So, I'm good to go now. Works great.

Thanks for taking the time, I'm fresh to WPF/Silverlight databinding and was trying to mimic CompanyEditor.xaml in the Rolodex app.

Chris

 

 



Copyright (c) Marimer LLC