绑定到列表的文本框不会更新

时间:2011-06-03 12:51:29

标签: wpf list binding static

HI, 我有一个带有日期的课程列表(由嵌套类返回)。由于我不确定这个人的课程是多少,我只是将它绑定到文本框并且工作正常。网格的datacontext是列表本身,因此我假设每当我对表单进行更改时,它都会自动发送回列表并添加它,但是当需要将其保存在DB中时,列表不会更改。我尝试将列表作为可观察集合和绑定模式两种方式,但仍然不会改变。当我尝试放置Onpropertychange时,它说“无法通过嵌套类型访问外部类型的非静态成员......”我想要做的是每当我输入/添加到文本框中的内容时,列表会将其添加为第n个项目,所以我可以迭代并将其保存在DB中。

还有其他办法吗?请看下面的代码。谢谢!

    #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

现在这是我的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>

2 个答案:

答案 0 :(得分:2)

在你的ListCon属性中,你总是会返回一个新的集合,这就完全违背了ObservableCollection,你应该把它存储在这样的字段中:

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

答案 1 :(得分:0)

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);
            }

然后只保存包含数据的那些:

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