WPF ComboBox refresh issue

WPF ComboBox refresh issue

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


FernandoI posted on Tuesday, October 13, 2009

 Hello, I am new to WPF but have been using CSLA for a while now.

I am creating a proof of concept app and I am running into a frustrating issue, that I am sure is my lack of wpf knowledge.

I have a List that I am using to populate a combo box. Code for the list is below.

I also have a combo box I am updateing in code, although declaritive XAML would be fine as well. I just cant get it to work. The binding actually works and I can see my list populated in the combo box however, If I add a new item to the list, I cannot get the combo box to update and show the new item.

Thanks in advance for any help.

Binding binding = new Binding();

Csla.Wpf.CslaDataProvider _dp = new Csla.Wpf.CslaDataProvider();

_dp.ObjectType = typeof(DD_Manufacturer);

_dp.FactoryMethod = "GetList";

_dp.ManageObjectLifetime = true;

binding.Source = _dp;

CB_Manufacturer.DisplayMemberPath = "Value";

CB_Manufacturer.SetBinding(ListView.ItemsSourceProperty, binding);

////////////////////////////////

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;

using System.Data;

using Csla;

using Csla.Data;

using BusinessLayer.DataBase;

namespace BusinessLayer.Library.DropDown

{

[Serializable]

public class DD_Manufacturer : Csla.NameValueListBase<int, string>

{

#region Business Methods

public static int DefaultConversions()

{

DD_Manufacturer list = GetList();

if (list.Count > 0)

return list.Items[0].Key;

else

throw new NullReferenceException(

"No roles available; default role can not be returned");

}

#endregion

#region Factory Methods

private static DD_Manufacturer _list;

/// <summary>

/// Returns a list of conversions.

/// </summary>

public static DD_Manufacturer GetList()

{

return DataPortal.Fetch<DD_Manufacturer>();

//if (_list == null)

// _list = DataPortal.Fetch<DD_Manufacturer>

// (new Criteria(typeof(DD_Manufacturer)));

//return _list;

}

/// <summary>

/// Clears the in-memory length conversions cache

/// so the list of conversions is reloaded on

/// next request.

/// </summary>

public static void InvalidateCache()

{

_list = null;

}

private DD_Manufacturer()

{ /* require use of factory methods */ }

#endregion

#region Data Access

private void DataPortal_Fetch()

{

this.RaiseListChangedEvents = false;

using (SqlConnection cn = new SqlConnection(Database.WindowSolutionsConnection))

{

cn.Open();

using (SqlCommand cm = cn.CreateCommand())

{

cm.CommandType = CommandType.StoredProcedure;

cm.CommandText = "DD_Manufacturer_Fetch";

using (SafeDataReader dr =

new SafeDataReader(cm.ExecuteReader()))

{

IsReadOnly = false;

while (dr.Read())

{

this.Add(new NameValuePair(

dr.GetInt32("ID"), dr.GetString("ManufacturerName")));

}

IsReadOnly = true;

}

}

}

this.RaiseListChangedEvents = true;

}

#endregion

}

}

ajj3085 replied on Tuesday, October 13, 2009

If you're using code to setup your bindings, you're doing it wrong. You should be able to setup all your bindings in pure Xaml.

WPF Unleased from SAMS is a great resource, if you don't already have it.

FernandoI replied on Tuesday, October 13, 2009

Thanks for the response.

I have also setup the binding in xaml with the sam exact results. I can see the data in my combo box but if the underlying table is updated, my combo box does not refresh. Here is the Xaml markup I was using.

What am I doing wrong?

 

Any help would be greatly appreciated.

 <Window.Resources>
        <csla:CslaDataProvider x:Key="_dp"
                               ObjectType="{x:Type ws:Manufacturers}"
                               FactoryMethod="GetManufacturers"
                               IsInitialLoadEnabled="True">           
        </csla:CslaDataProvider>
        <Binding Mode="TwoWay"  BindsDirectlyToSource="True" Source="_dp" x:Key="binding">
           
        </Binding>
    </Window.Resources>

 

ItemsSource="{Binding binding, Mode=TwoWay}" DisplayMemberPath="ManufacturerName"

ajj3085 replied on Tuesday, October 13, 2009

Ok, I see what you're trying to do. How does your Manufacturers instance update itself? Also are you use BindsDirectlyToSource should be set?

FernandoI replied on Tuesday, October 13, 2009

Hello, I am not sure if bindsdirectly to source should be set. originally I did not have it set and thought I would give it a try to no avail.

what I am doing to update Manufacturer is calling the object below.

The functionality I am trying to capture is this.. if a user does not see his item in the combo box, I have a form that allow him to enter in a new Manufacturer. It simply uses a business object to write back to my database. This works fine.

 

I hope you can pick out where I have gone wrong. I am at a loss now.

Thanks for looking at this for me. I really appreciate the time.

using Csla;

using System;

using Csla.Data;

namespace BusinessLayer.Library.Admin

{

[Serializable()]

public class Manufacturer : BusinessBase<Manufacturer>

{

#region Business Methods

private byte[] _timestamp = new byteMusic [8];

private static PropertyInfo<int> IDProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<int>("ID"));

[System.ComponentModel.DataObjectField(true, true)]

public int Id

{

get { return GetProperty(IDProperty); }

set { SetProperty(IDProperty, value); }

}

private static PropertyInfo<string> ManufacturerNameProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("ManufacturerName"));

public string ManufacturerName

{

get { return GetProperty(ManufacturerNameProperty); }

set { SetProperty(ManufacturerNameProperty, value); }

}

private static PropertyInfo<string> CityProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("City"));

public string City

{

get { return GetProperty(CityProperty); }

set { SetProperty(CityProperty, value); }

}

private static PropertyInfo<string> StateProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("State"));

public string State

{

get { return GetProperty(StateProperty); }

set { SetProperty(StateProperty, value); }

}

private static PropertyInfo<string> ZipProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Zip"));

public string Zip

{

get { return GetProperty(ZipProperty); }

set { SetProperty(ZipProperty, value); }

}

private static PropertyInfo<string> AddressProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Address"));

public string Address

{

get { return GetProperty(AddressProperty); }

set { SetProperty(AddressProperty, value); }

}

private static PropertyInfo<string> Phone1Property =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Phone1"));

public string Phone1

{

get { return GetProperty(Phone1Property); }

set { SetProperty(Phone1Property, value); }

}

private static PropertyInfo<string> Phone2Property =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Phone2"));

public string Phone2

{

get { return GetProperty(Phone2Property); }

set { SetProperty(Phone2Property, value); }

}

private static PropertyInfo<string> ContactFirstNameProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Contact_First_Name"));

public string Contact_First_Name

{

get { return GetProperty(ContactFirstNameProperty); }

set { SetProperty(ContactFirstNameProperty, value); }

}

private static PropertyInfo<string> ContactLastNameProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Contact_Last_Name"));

public string Contact_Last_Name

{

get { return GetProperty(ContactLastNameProperty); }

set { SetProperty(ContactLastNameProperty, value); }

}

private static PropertyInfo<string> EmailProperty =

RegisterProperty(typeof(Manufacturer), new PropertyInfo<string>("Email"));

public string Email

{

get { return GetProperty(EmailProperty); }

set { SetProperty(EmailProperty, value); }

}

public override string ToString()

{

return Id.ToString();

}

#endregion

#region Validation Rules

protected override void AddBusinessRules()

{

//ValidationRules.AddRule<Manufacturer>(Assignment.ValidRole, RoleProperty);

}

#endregion

#region Authorization Rules

protected override void AddAuthorizationRules()

{

//AuthorizationRules.AllowWrite(RoleProperty, "ProjectManager");

}

#endregion

#region Factory Methods

public static Manufacturer NewManufacturer()

{

return DataPortal.Create<Manufacturer>();

}

public static Manufacturer GetManufacturer(WindowSolutions.DalLinq.Manufacturer_FetchResult data)

{

return DataPortal.FetchChild<Manufacturer>(data);

}

private Manufacturer()

{ /* require use of factory methods */ }

#endregion

#region Data Access

[RunLocal]

protected override void DataPortal_Create()

{

Id = 0;

ManufacturerName = string.Empty;

City = string.Empty;

State = string.Empty;

Zip = string.Empty;

Address = string.Empty;

Phone1 = string.Empty;

Phone2 = string.Empty;

Contact_First_Name = string.Empty;

Contact_Last_Name = string.Empty;

Email = string.Empty;

}

private void Child_Fetch(WindowSolutions.DalLinq.Manufacturer data)

{

Id = data.ID;

ManufacturerName = data.ManufacturerName;

City = data.City;

State = data.State;

Zip = data.Zip;

Address = data.Address;

Phone1 = data.Phone1;

Phone2 = data.Phone2;

Contact_First_Name = data.Contact_First_Name;

Contact_Last_Name = data.Contact_Last_Name;

Email = data.Email;

}

protected void DataPortal_Insert()

{

using (var mgr = ContextManager<WindowSolutions.DalLinq.QuickQuoteDataContext>.GetManager(WindowSolutions.DalLinq.Database.WindowSolutions))

{

//System.Data.Linq.Binary lastChanged = _timestamp;

mgr.DataContext.Manufacturer_Insert(ManufacturerName, City, State, Zip, Address, Phone1, Phone2, Contact_First_Name, Contact_Last_Name, Email);

}

}

//private void Child_Update(Resource resource)

//{

// //_timestamp = Assignment.UpdateAssignment(_projectId, resource.Id, _assigned, _role, _timestamp);

//}

//private void Child_DeleteSelf(Resource resource)

//{

// //Assignment.RemoveAssignment(_projectId, resource.Id);

//}

#endregion

}

}

ajj3085 replied on Tuesday, October 13, 2009

Ahh, I have a similar requirement, but I went about it differently.

The BO with the company value has that value as a string. The list of companies is only used for the suggestahead typing. The BO it self is responsible for finding an existing company with the same name, or creating the new company record.

You might have an easier time if you head down that route.

FernandoI replied on Tuesday, October 13, 2009

Thanks, I was hoping for a way to just have underlying data changes reflect in my combobox.

I can do this with win forms and .net web apps with no problem. Not sure what I am doing wrong with WPF though.

 

 

Copyright (c) Marimer LLC