简体   繁体   English

在xmal中进行数据绑定时数据网格未更新

[英]Data grid not update when data binding in xmal

I have a model called TimeSheetByEmployeeModel that is implemented with INotifyPropertyChanged . 我有一个称为TimeSheetByEmployeeModel的模型,该模型通过INotifyPropertyChanged实现。

Model: 模型:

public class TimeSheetByEmployeeModel:INotifyPropertyChanged
{
    //public string EmpID { get; set; }
    string _EmpID;
    public string EmpID
    {
        get
        {
            return _EmpID;
        }
        set
        {
            if (_EmpID != value)
            {
                _EmpID = value;
                RaisePropertyChange("EmpID");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChange(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));

        }
    }
}

ViewModel: ViewModel:

ObservableCollection<TimeSheetByEmployeeModel> _TimeSheetList = new ObservableCollection<TimeSheetByEmployeeModel>();
public ObservableCollection<TimeSheetByEmployeeModel> TimeSheetList
{
    get
    {
        return _TimeSheetList;
    }
    set
    {                
        _TimeSheetList = value;             
    }
}

View Code-Behind: 查看隐藏代码:

public TimeSheetByEmployee()
{
    InitializeComponent();          

    this.DataContext = this;
}

View: 视图:

<DataGrid  ItemsSource="{Binding TimeSheetList,UpdateSourceTrigger=PropertyChanged}"
           AutoGenerateColumns="False"
           CanUserAddRows="False"
           x:Name="timesheetgrid"
           HeadersVisibility="All"
           RowHeaderWidth="20">

  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding EmpID,UpdateSourceTrigger=PropertyChanged}" 
                    Header="EmpID"
                    IsReadOnly="True"
                    Width="100"/>
  </DataGrid.Columns>  
</DataGrid>

When I click a button inside the View it calls this method: 当我单击视图内的按钮时,它将调用此方法:

private void Ok_Click(object sender, RoutedEventArgs e)
{       
    try
    {
        TimeSheetList.Clear();
        string EmployeeID = cboemployee.SelectedValue.ToString();
        TimeSheetList = TimeSheetByEmployeeDataAccessor.GetTimeSheetByEmployee_ByFromTo(dtpfrom.SelectedDate.GetValueOrDefault(), dtpto.SelectedDate.GetValueOrDefault(), EmployeeID);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }          
}

I dynamically changed TimeSheetList and it should reflect in the datagrid. 我动态更改了TimeSheetList ,它应该反映在datagrid中。 I developed another form using this structure and it worked fine. 我使用这种结构开发了另一种形式,并且效果很好。 However, in this form, when I change TimeSheetList it does not update the datagrid. 但是,以这种形式,当我更改TimeSheetList它不会更新datagrid。

What have I researched? 我研究了什么?

Firstly, I bound some data to TimeSheeList in the view constructor and it's shown in the datagrid. 首先,我将一些数据绑定到视图构造函数中的TimeSheeList ,并将其显示在datagrid中。 So I assumed binding TimeSheetList to the datagrid works. 因此,我假定将TimeSheetList绑定到datagrid工程。

Secondly, I changed some code in the Button Click Event: 其次,我在Button Click Event中更改了一些代码:

private void Ok_Click(object sender, RoutedEventArgs e)
{       
    try
    {      
        TimeSheetList.RemoveAt(0);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }          
}

This code is works perfectly and removes the first row in the datagrid. 这段代码非常完美,可以删除数据网格中的第一行。 So I assumed the datagrid itemsource is dynamically changing. 因此,我假设datagrid项源是动态变化的。

But why does it not update with my original code? 但是为什么不使用我的原始代码进行更新?

More research, 更多研究,

I added code to the button: 我向按钮添加了代码:

timesheetgrid.ItemsSource = TimeSheetList;

I know it will work, and it does, but normally I don't databind to the datagrid in code-behind and I always databind in the XAML. 我知道它会工作,而且确实可以,但是通常我不会在后台代码中将数据绑定到数据网格,而我总是在XAML中进行数据绑定。 It always works, but why doesn't it work here? 它始终有效,但为什么在这里不起作用?

Update 更新资料

If I add Mode=TwoWay to the ItemsSource databinding in the datagrid in xaml while debugging like this: 如果在调试时将Mode=TwoWay添加到xaml的datagrid中的ItemsSource数据绑定中,则如下所示:

<DataGrid  
    ItemsSource="{Binding TimeSheetList,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
    AutoGenerateColumns="False"
    CanUserAddRows="False"
    x:Name="timesheetgrid"
    HeadersVisibility="All"
    RowHeaderWidth="20">

It works and the datagrid shows data. 它可以工作,并且datagrid显示数据。 So I saved and restarted the debugging process and it stops working again. 因此,我保存并重新启动了调试过程,它再次停止工作。

this line: 这行:

TimeSheetList = TimeSheetByEmployeeDataAccessor.GetTimeSheetByEmployee_ByFromTo(dtpfrom.SelectedDate.GetValueOrDefault(), dtpto.SelectedDate.GetValueOrDefault(), EmployeeID);

creates a new collection and there is no notification in setter about change: 创建一个新集合,setter中没有有关更改的通知:

set
{
    _TimeSheetList = value;
}

I suggest to avoid changing collection and add items to existing collection: 我建议避免更改集合并将项目添加到现有集合:

var items = TimeSheetByEmployeeDataAccessor.GetTimeSheetByEmployee_ByFromTo(dtpfrom.SelectedDate.GetValueOrDefault(), dtpto.SelectedDate.GetValueOrDefault(), EmployeeID);
TimeSheetList.Clear();
foreach(var item in items)
    TimeSheetList.Add(item);

... or if you prefer to change collection (like it is done now), then raise notification ...或者如果您想更改收藏集(如现在完成),则发出通知

set
{
    _TimeSheetList = value;
    RaisePropertyChange("TimeSheetList");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM