My first StackExchange question, so tell me if I do anything wrong.
I'm converting old winforms code to WPF, and am leaning them both along the way. I've been successful most of the time, but I'm tacking my first Data Table involving Data Binding. It seems to be the hardest thing to convert since binding is handled mostly in xaml rather than code.
As an additional note, it uses CSLA, which to my understanding is a code structure similar to MVVM(still learning what these are).
With a bit of experimenting, I managed to create a functioning data grid, but I need to implement one of the columns as a combo box. It is represented currently as a number(1-10), but I need the description that goes with the number, which is in a different table in the database.
The number that was originally displayed before switching it to a combobox was noneligibilityreason. I want this value to be the currently selected item in the combobox.
Can't post images, so here's my attempt at showing the structure of the database tables:
modreview
- reviewnum
- intcaseno
- newreferral
- screendate
- eligible
- noneligibilityreason(this is the one to make into a combobox)
noneligreason(this is the subtable that aligns with noneligibilityreason)
- noneligreasonid
- noneligreasondesc
The code included this function, which seems to create a collection that should be, I think, the ItemsSource:
NERList.GetNameValueList();
As for the ItemsSource, I want all of the noneligreasondesc values. Of course then there is the matter of getting the noneligibilityreason to match up to the noneligreasonid. I found this example, which seems to do the same thing, but without the database, using classes instead:
I tried to duplicate this as I thought it should work here in my XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTestProject" x:Class="WpfTestProject.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<local:caseviewDataSet x:Key="caseviewDataSet"/>
<CollectionViewSource x:Key="modreviewViewSource"
Source="{Binding modreview, Source={StaticResource caseviewDataSet}}"/>
<CollectionViewSource x:Key="noneligibilityViewSource"
Source="{Binding noneligreason, Source={StaticResource caseviewDataSet}}"/>
</Window.Resources>
<Grid DataContext="{StaticResource modreviewViewSource}">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid x:Name="modreviewDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
ItemsSource="{Binding}"
EnableRowVirtualization="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="reviewnumColumn" Width="SizeToHeader" Header="reviewnum" Binding="{Binding reviewnum}"/>
<DataGridTextColumn x:Name="intcasenoColumn" Width="SizeToHeader" Header="intcaseno" Binding="{Binding intcaseno}"/>
<DataGridCheckBoxColumn x:Name="newreferralColumn" Width="SizeToHeader" Header="newreferral" Binding="{Binding newreferral}"/>
<DataGridTemplateColumn x:Name="screendateColumn" Width="Auto" Header="screendate">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding screendate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn x:Name="eligibleColumn" Width="SizeToHeader" Header="eligible" Binding="{Binding eligible}"/>
<DataGridComboBoxColumn x:Name="noneligibilityreasonColumn" Width="SizeToHeader" Header="noneligibilityreason"
ItemsSource="{Binding Source={StaticResource noneligibilityViewSource}}"
SelectedValueBinding="{Binding noneligibilityreason}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
/>
</DataGrid.Columns>
</DataGrid>
</Grid>
And the code behind:
using CslaFactoryBusinessObjects;
...
namespace WpfTestProject
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WpfTestProject.caseviewDataSet caseviewDataSet = ((WpfTestProject.caseviewDataSet)(this.FindResource("caseviewDataSet")));
// Load data into the table modreview. You can modify this code as needed.
WpfTestProject.caseviewDataSetTableAdapters.modreviewTableAdapter caseviewDataSetmodreviewTableAdapter = new WpfTestProject.caseviewDataSetTableAdapters.modreviewTableAdapter();
caseviewDataSetmodreviewTableAdapter.Fill(caseviewDataSet.modreview);
System.Windows.Data.CollectionViewSource modreviewViewSource = ((CollectionViewSource)(this.FindResource("modreviewViewSource")));
modreviewViewSource.View.MoveCurrentToFirst();
noneligibilityreasonColumn.ItemsSource = NERList.GetNameValueList();
}
}
}
Most of this code was generated when I drag dropped from Data Sources, but the last line in the code behind is where I think I was supposed to add the ItemsSource. Not sure if it belongs in Loaded, but it seems like it might be OK there. Also not sure if there is a way to do this in XAML instead.
I realize I duplicated setting ItemsSource in XAML and code, but neither one works properly, so I included both to show what options I have tried.
Finally, I think I should show the old winforms code that I'm trying to emulate in the WPF conversion(Not sure if this is enough of the code). I think it uses a hidden combobox to set up the binding, then adds it to the table:
//from Program.cs used in setupModRvwGrdHdr()
public static void ListControlBinding(ref UltraCombo comboBox, object lkupdataSource, string displayMember,
string valueMember, object objDataSource, string objProp) {
comboBox.DataSource = lkupdataSource;
comboBox.DisplayMember = displayMember;
if (!string.IsNullOrEmpty(valueMember))
comboBox.ValueMember = valueMember;
if (objDataSource != null)
comboBox.DataBindings.Add("Value", objDataSource, objProp);
}
//from the code for the specific winform
private void setupModRvwGrdHdr() {
cbNonEligReason.DataBindings.Clear();
grdModReviews.DataSource = bsModRvws;
Program.ListControlBinding(ref cbNonEligReason, NERList.GetNameValueList(), "Value", "Key", bsModRvws,
"NonEligibleReasonIDStr");
cbNonEligReason.DisplayLayout.Bands[0].Columns["Key"].Hidden = true;
cbNonEligReason.DisplayLayout.Bands[0].Columns["Value"].Header.Caption = "Noneligiblity Reason";
grdModReviews.DisplayLayout.Bands[0].Columns["reviewnum"].CellActivation = Activation.NoEdit;
grdModReviews.DisplayLayout.Bands[0].Columns["intcaseno"].Hidden = true;
grdModReviews.DisplayLayout.Bands[0].Columns["noneligibilityreason"].Hidden = true;
grdModReviews.DisplayLayout.Bands[0].Columns["screendate"].Hidden = false;
grdModReviews.DisplayLayout.Bands[0].Columns["screendate"].Header.Caption = "Date of Screen";
grdModReviews.DisplayLayout.Bands[0].Columns["screendate"].Width = 100;
grdModReviews.DisplayLayout.Bands[0].Columns["screendate"].EditorComponent = dteModRvwDate;
grdModReviews.DisplayLayout.Bands[0].Columns["eligible"].Hidden = false;
grdModReviews.DisplayLayout.Bands[0].Columns["eligible"].Header.Caption = "Eligible";
grdModReviews.DisplayLayout.Bands[0].Columns["eligible"].Width = 70;
grdModReviews.DisplayLayout.Bands[0].Columns["NonEligibleReasonIDStr"].Hidden = false;
grdModReviews.DisplayLayout.Bands[0].Columns["NonEligibleReasonIDStr"].Header.Caption =
"Reason for Noneligibility";
grdModReviews.DisplayLayout.Bands[0].Columns["NonEligibleReasonIDStr"].Width = 250;
grdModReviews.DisplayLayout.Bands[0].Columns["NonEligibleReasonIDStr"].EditorComponent = cbNonEligReason;
grdModReviews.DisplayLayout.Bands[0].Columns["NonEligibleReasonIDStr"].Nullable =
Infragistics.Win.UltraWinGrid.Nullable.Nothing;
}
This is a fairly large program, and there might be some helpful functions in the CslaFactoryBusinessObjects classes, but I think I should learn how to do this to better understand data manipulation in WPF.
I've been looking for a solution for days now and haven't found a case similar enough to mine. I'm just not sure exactly how each of the binding properties works and how they apply in this specific case. Finally had to throw in the towel and make an account here. Sorry for the length, but I wanted to be specific and show that I have been at this for a while.
Please Help!