wpf and databinding

wpf and databinding

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


fli1973 posted on Tuesday, October 27, 2009

I have a wpf app with a bound combo box.
The combo box lists Manufacturers.
Ford
Chevy
GM

I am trying to update the combo box when a new manufacturer is added to the database. I use a BusinessListBase object to populate my combo box. Here is the binding code.

-------------data provider Begin-----------
ObjectType="{x:Type QuickQuote:Manufacturers}"
FactoryMethod="GetManufacturers"
IsInitialLoadEnabled="True"
/>

---------------data provider end---------------

-----------------combo box begin--------------
ItemsSource="{Binding Source={StaticResource ManufacturerDropDown}}" DisplayMemberPath="ManufacturerName" SelectedValuePath="Id"/>

----------------combo box end-----------------

What am I doning Wrong here?

RockfordLhotka replied on Wednesday, October 28, 2009

Are you reloading the collection object to get the new data from the database? Or is that the collection you are directly editing? And if that is the collection you are editing, are you editing it by using the same data provider?

The data provider should notify the UI controls when the business object is changed or saved, but that only works if everything is bound to the same data provider.

The CslaDataProvider control has Refresh() and Rebind() methods. Refresh() reloads the data by invoking the factory method again. Rebind() just resets the UI bindings to get the UI to update its display.

It is possible that the ComboBox control doesn't automatically rebind when the data provider changes. I don't know one way or the other, but it that is possible - in which case calling Rebind() might solve the issue.

fli1973 replied on Wednesday, October 28, 2009

Thank you for the response. I am not directly editing the collection. I have a different BO that handles adding a new manufacturer.

----------------------------------------------------------
BusinessLayer.Library.Admin.Manufacturer _man = BusinessLayer.Library.Admin.Manufacturer.NewManufacturer();
_man.ManufacturerName = Txt_Manufacturer.Text;
_man.City = Txt_City.Text;
_man.State = Txt_State.Text;
_man.Zip = Txt_Zip.Text;
_man.Address = Txt_Address.Text;
_man.Phone1 = Txt_Phone1.Text;
_man.Phone2 = Txt_Phone2.Text;
_man.Contact_First_Name = Txt_FirstName.Text;
_man.Contact_Last_Name = Txt_LastName.Text;
_man.Email = Txt_Email.Text;
_man.Save();
this.Close();
MainWindow _main = new MainWindow();
_main.ManufacturerDropDown();

----------------------------------------------------------

Immediately after adding the new manufacturer I call a method that refreshes my combo box.

public void ManufacturerDropDown()
{
Csla.Wpf.CslaDataProvider _dp = this.FindResource("ManufacturerDropDown") as Csla.Wpf.CslaDataProvider;
_dp.Refresh();
_dp.Rebind();
}

The refresh does reload my data, but I cant get it to refresh my combo box.

I am sure it is something simple that I have been overlooking.

Thanks again for you help

richardb replied on Wednesday, October 28, 2009

I use a NameValueListBase object for showing items in a combo rather than a BusinessListBase.  Is there any other reason why you want BusinessListBase?

I'd typcially have separate Manufacturers and Manufacturer objects for maintaining those records.  Then, when a manufacturer was added, updated or removed from the database I'd get it to InvalidateCache on the ManufacturerNVList so next time the updated list would be retrieved from the database.

<csla:CslaDataProvider x:Key="SectorsList" ObjectType="{x:Type POSTracker:SectorsList}"
FactoryMethod
="GetList"
IsAsynchronous="True" />

<ComboBox Grid.Row="0" Grid.Column="1" Name="cboSector"
ItemsSource="{Binding Source={StaticResource SectorsList}}"
DisplayMemberPath
="Value"
SelectedValuePath
="Key"
SelectedValue="{Binding Path
=SectorID}"
Width="150" IsEnabled="True"/>

<csla:PropertyStatus Property="{Binding Path=SectorID, Mode=TwoWay}" Grid.Row="0" Grid.Column="2"
TargetControl="{Binding ElementName=cboSector}" />

fli1973 replied on Wednesday, October 28, 2009

Thank you for the response. I just setup a NameValueList instead of businessListBase with the same results. I cant get the combo box to display the new data.

------------------------------
ObjectType="{x:Type QuickQuote:DD_Manufacturer}"
FactoryMethod="GetList"
IsInitialLoadEnabled="True"
/>

-----------------------------------------------------

----------------------------------------------------
ItemsSource="{Binding Source={StaticResource ManufacturerDropDown}}" DisplayMemberPath="Value" SelectedValuePath="Key" />
--------------------------------------------------------

After adding a new Manufacturer I call this method

----------------------------------------
public void ManufacturerDropDown()
{
Csla.Wpf.CslaDataProvider _dp = this.FindResource("ManufacturerDropDown") as Csla.Wpf.CslaDataProvider;
_dp.Refresh();
_dp.Rebind();
}

-------------------------------------------------

Is this the correct way to invalidate the list?

Thank you so much for the help.

RockfordLhotka replied on Wednesday, October 28, 2009

Using the debugger, can you confirm that the list ends up with the new data?

In other words, let's try to narrow down the problem - first making sure
that the list actually does have the updated data.

That'll help identify whether the problem is a simple data access issue, or
something to do with XAML binding.

fli1973 replied on Wednesday, October 28, 2009

I added a break point at my GetManufacturers Method and it is hit at load as well as at Refresh(). It does have the new data. It has been tough moving from winforms to WPF. Thanks again,

RockfordLhotka replied on Wednesday, October 28, 2009

Hmm, at this point I don't have any real answer. Clearly the issue is
something to do with data binding and the ComboBox not recognizing that it
needs to refresh its items...

fli1973 replied on Wednesday, October 28, 2009

I appreciate you taking a look at this. I will post a solution if I can figure it out.

Fernando

richardb replied on Thursday, October 29, 2009

Maybe try clearing factory parameters and  resetting the FactoryMethod list.

Here's some code (VB) I use to reset the values on a combo based upon the selection made in another combo.

 

Dim dpSubType As Csla.Wpf.CslaDataProvider

dpSubType = TryCast(Me.FindResource("QualificationSubTypeList"), Csla.Wpf.CslaDataProvider)

Using dpSubType.DeferRefresh

dpSubType.FactoryParameters.Clear()

dpSubType.FactoryMethod = "GetList"

dpSubType.FactoryParameters.Add(qualTypeID)

End Using

Copyright (c) Marimer LLC