I'm got a problem that's really got me stumped. I want to have a ListBox on a page. The ItemsSource for that list box I want bound to a lookup table. I'll display a description field from the lookup table. That's all a fine and good; not hard to do.
What makes it harder is that I want to add a checkbox to each row in the listbox. Each checkbox is either checked, or not, if there's a value stored in another table (a data table) corresponding to the ID value from the lookup table, which is in the data table as a foreign key to the lookup table. I've written a value converter, but believe me this is getting way too complicated, and ultimately I don't think this is going to work. Here's the C# for the value converter:
[ValueConversion(typeof(byte), typeof(bool))] public class ClientProbConverter : IValueConverter { public int ClientNumber { get; set; } public byte CaseNumber { get; set; } public byte AdlProblem { get; set; } #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { byte adlProblem = (byte)value; bool ret = false; ASIEntities asiContext = new ASIEntities(); var asiAdlProblem = asiContext.ASIADLProblems.Where(c => c.ClientNumber == ClientNumber && c.CaseNumber == CaseNumber && c.ADLProblem == adlProblem).FirstOrDefault(); if (asiAdlProblem != null) { ret = true; } asiContext.Dispose(); return ret; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { bool tmp = (bool)value; ASIEntities asiContext = new ASIEntities(); var asiAdlProblem = asiContext.ASIADLProblems.Where(c => c.ClientNumber == ClientNumber && c.CaseNumber == CaseNumber && c.ADLProblem == AdlProblem).FirstOrDefault(); if (tmp) { if (asiAdlProblem == null) { var prob = new ASIADLProblem(); prob.ClientNumber = ClientNumber; prob.CaseNumber = CaseNumber; prob.Followup = 0; prob.ADLProblem = AdlProblem; asiContext.ASIADLProblems.AddObject(prob); asiContext.SaveChanges(); } } else { //the checkbox was cleared if (asiAdlProblem != null) { asiContext.DeleteObject(asiAdlProblem); asiContext.SaveChanges(); } } asiContext.Dispose(); return AdlProblem; } #endregion }
As you can see, I have 3 public properties to this ClientProbConverter class: ClientNumber, CaseNumber and AdlProblem. All 3 comprise the primary key of the data table, with ADLProblem being the foreign key to the lookup table. ASIEntities is an entity framework class I wrote for fetching/saving the data to the database. I had hoped that I could somehow bind ClientNumber, CaseNumber and ADLProblem, from the data table, to the check box in each row of the list box, but I'm not sure I can.
Here's the XAML for the listbox:
<ListBox x:Name="listOfProblems" Grid.Row="1" HorizontalAlignment="Left" AllowDrop="True" FontSize="16" ItemsSource="{Binding Source={StaticResource aSICodesADLProblemViewSource}}"><ListBox.ItemTemplate><DataTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="200" /><ColumnDefinition /></Grid.ColumnDefinitions><TextBlock Text="{Binding ADLProblemDesc}" /><!-- the next textblock will eventually be hidden --><TextBlock Grid.Column="1" Text="{Binding ADLProblem}" /></Grid></DataTemplate></ListBox.ItemTemplate></ListBox>aSICodesADLProblemViewSource is the CollectionViewSource for the lookup table. ADLProblemDesc and ADLProblem, in this context is coming from the lookup table (ASICodesADLProblem). I want to add a checkbox to this listbox, which will be related to the data table (ASIADLProblems in the EF context), but I'm completely stumped as to how to do it, and I honestly think what I've got so far is too complicated and doesn't do the job. How do I accomplish what I'm trying to do? Or should I approach this differently?
Rod