user742102
user742102

Reputation: 1365

Textbox binding to list won't update

HI, I have a list of coursesTaken with dates (returned by a nested class). Since I am not sure howmany courses the person took, I just bind it to textboxes and is working fine. The datacontext of the grid is the list itself so I assume whenever I make changes to the form, it is automatically sent back to the list and add it, but when it is time to save it in DB, the list won't change. I tried making the list as an observable collection and bind mode two way but still won't change. when I try to put Onpropertychange it says "Cannot acces a non static member of outer type via nested type..." What I want to do is whenever I type / add something to textbox, the list will add it as it's nth item so I can iterate and save it in DB.

is ther any other way to do this? please see code below. thanks!

    #region class for binding purposes (nested class)
    public class ListCon
    {
        public ObservableCollection<tblGALContinuingEducationHistory> EducList
        {
            get
            {

                return new ObservableCollection<tblGALContinuingEducationHistory>(contEducHstoryList);
            }

        }
    }
    #endregion

    #region constructor
    public ContinuingEducHistoryPopUp(tblAttorneyGalFileMaint currentGal)
    {

        contEducHstoryList = new ObservableCollection<tblGALContinuingEducationHistory>();
        InitializeComponent();

        if (currentGal != null)
        {
            CurrentGal = currentGal;
            txtMemberName.Text = CurrentGal.FullName;
            contEducHstoryList = new ObservableCollection<tblGALContinuingEducationHistory>(currentGal.tblGALContinuingEducationHistories.Count > 0 && currentGal.tblGALContinuingEducationHistories != null ? currentGal.tblGALContinuingEducationHistories : null);
        }
        this.DataContext = new ListCon();


    }
    #endregion

Now here's my xaml

<Grid Grid.Row="2" Grid.ColumnSpan="2" Name="grdEducForm">
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding Path=EducList[0].CourseTitle}" Grid.Column="0" Grid.Row="0" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[0].CertificateDate}" Grid.Column="1" Grid.Row="0" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[0].CertificateExpiration}" Grid.Column="2" Grid.Row="0" />

    <TextBox Text="{Binding EducList[1].CourseTitle}" Grid.Column="0" Grid.Row="1" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[1].CertificateDate}" Grid.Column="1" Grid.Row="1" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[1].CertificateExpiration}" Grid.Column="2" Grid.Row="1" />

    <TextBox Text="{Binding Path=EducList[2].CourseTitle}" Grid.Column="0" Grid.Row="2" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[2].CertificateDate}" Grid.Column="1" Grid.Row="2" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[2].CertificateExpiration}" x:Name="dpEnd3"
            Grid.Column="2" Grid.Row="2" />

    <TextBox Text="{Binding Path=EducList[3].CourseTitle,  Mode=TwoWay}" Grid.Column="0" Grid.Row="3" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[3].CertificateDate, Mode=TwoWay}" Grid.Column="1"
            Grid.Row="3" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[3].CertificateExpiration}" Grid.Column="2" Grid.Row="3" />

    <TextBox Text="{Binding Path=EducList[4].CourseTitle}" Name="txtEducName5" Grid.Column="0" Grid.Row="4" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[4].CertificateDate}" x:Name="dpStart5" Grid.Column="1"
            Grid.Row="4" />
    <useable:MaskedDatePicker DateValue="{Binding Path=EducList[4].CertificateExpiration}" x:Name="dpEnd5"
            Grid.Column="2" Grid.Row="4" />
</Grid>

Upvotes: 0

Views: 664

Answers (2)

user742102
user742102

Reputation: 1365

EducList = new ObservableCollection<tblGALContinuingEducationHistory>(currentGal.tblGALContinuingEducationHistories.Count > 0 && currentGal.tblGALContinuingEducationHistories != null ? currentGal.tblGALContinuingEducationHistories : null);

            int count = 0;
           //5 is the maximum no of course an atty can take and save in this form   
            count = 5 - EducList.Count;
            for (int i = 0; i < count; i++)
            {
                galContEdu = FileMaintenanceBusiness.Instance.Create<tblGALContinuingEducationHistory>();
                galContEdu.AttorneyID = currentGal.AttorneyID;
                EducList.Add(galContEdu);
            }

then save only the ones that has data:

foreach (var item in EducList)
            {
               if(!string.IsNullOrEmpty(item.CourseTitle) || !string.IsNullOrWhiteSpace(item.CourseTitle))
                FileMaintenanceBusiness.Instance.SaveChanges(item, true);
            }

Upvotes: 0

brunnerh
brunnerh

Reputation: 185057

In your ListCon property you always return a new collection this defeats the point of having an ObservableCollection at all, you should store it in a field like this:

//Initialize field in constructor
private readonly ObservableCollection<tblGALContinuingEducationHistory> _ListCon;
public ObservableCollection<tblGALContinuingEducationHistory> ListCon
{
    get
    {
        return _ListCon;
    }
}

Upvotes: 2

Related Questions