Archive for the ‘WPF’ Category

WPF databinding

June 16, 2010

ok, so I never got the databinding to work quite right between my listbox and the various add,update,delete operations I was doing with Linq to SQL.

However, I did get it all to look good, with the listbox getting updated, and this is how I did it.

The magic function was: GetNewBindingList called from my linq table object.

Somehow it feels like the linq to sql class should inform the listbox that it needs to refresh and there should be some magic setting, but for the time being:
XAML extract:

set the datasource in the window:
<Window x:Class="HomerV3_Queries.Window_Queries"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
bind to it at the listbox. "Queries" is the table I'm binding to.:
ItemsSource="{Binding Path=Queries,UpdateSourceTrigger=PropertyChanged}"

C# code called after every database change – note the commented out stuff that does not work:

void PageRefresh()
{
//_context.SubmitChanges();

//_context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _context.Queries);
//lbQueries.UpdateLayout();
//lbQueries.ItemsSource = null;
//lbQueries.Items.Clear();
lbQueries.ItemsSource = _context.Queries.GetNewBindingList();
//lbQueries.Refresh();
lbQueries.SelectedIndex = -1;
txtQuery.Text = "";
txtTitle.Text = "";
}