使用 Caliburn Micro 更新 WPF 中的 DataGrid

Updating DataGrid in WPF with Caliburn Micro

我正在使用 Caliburn Micro 开发一个 WPF 项目。在这个应用程序中,我有一个 DataGrid,我使用 Dapper 使用来自 SQL 服务器数据库的数据填充它。请考虑以下代码片段:

ChangesModel.cs

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace PTSRDesktopUI.Models
{
    //public class for all changes attributes
    public class ChangesModel : INotifyPropertyChanged
    {
        public int ID { get; set; }
        public string Facility { get; set; }
        public string Controller { get; set; }
        public string ParameterName { get; set; }
        public string OldValue { get; set; }
        public string NewValue { get; set; }
        public DateTime ChangeDate { get; set; }

        private bool _validated;
        public bool Validated
        {
            get { return _validated; }
            set { _validated= value; NotifyPropertyChanged(); }
        }

        public DateTime? ValidationDate { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

OverviewViewmodel.cs

using Caliburn.Micro;
using PTSRDesktopUI.Helpers;
using PTSRDesktopUI.Models;

namespace PTSRDesktopUI.ViewModels
{
    public class OverviewViewModel : Screen
    {

        //Create new Bindable Collection variable of type ChangesModel
        public BindableCollection<ChangesModel> Changes { get; set; }


        public OverviewViewModel()
        {
            //Create connection to dataAccess class
            DataAccess db = new DataAccess();

            //get the changes from dataAccess function and store them as a bindabla collection in Changes
            Changes = new BindableCollection<ChangesModel>(db.GetChangesOverview());

            //Notify for changes
            NotifyOfPropertyChange(() => Changes);

        }

        //Validate_Btn click event
        public void Validate()
        {

            //TODO: Change CheckBox boolean value to true and update DataGrid
        }
    }
}

OverviewView.xaml

    <!--Datagrid Table-->
    <DataGrid Grid.Row="1" x:Name="Changes" CanUserAddRows="False" AutoGenerateColumns="False">
        <DataGrid.Columns>
        <!--..........-->
        <!--Some irrelevant code-->
        <!--..........-->
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="Validated_CheckBox" IsChecked="{Binding Path=Validated, UpdateSourceTrigger=PropertyChanged}" IsHitTestVisible ="False"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=ValidationDate, TargetNullValue='NaN',
                                StringFormat='{}{0:dd.MM HH:mm}'}"/>
            <DataGridTemplateColumn CellStyle="{StaticResource DataGridCellCentered}">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="Validate_Btn" cal:Message.Attach="[Event Click] = [Action Validate]"
                            Visibility="{Binding DataContext.Validated, 
                               Converter={StaticResource BoolToVisConverter}, RelativeSource={RelativeSource AncestorType=DataGridCell}}"
                            cal:Bind.Model="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}">Validate</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

我想完成的是: 当用户单击 Validate Button 时,CheckBox 的布尔值设置为 true,ValidationDate 设置为 now,并且更新 DataGrid。然后我将启动一个存储过程来更新数据库 table。另请注意,Button 仅在选中 CheckBox 时可见。所以我只想知道如何在 ViewModel 方法 Validate() 中访问 Validated 属性 和 ValidationDate。另外,我如何在更改 ValidatedValidationDate 的值后更新 DataGrid,以便如果我打开另一个 ContentControl,这些值不会重置? 谁有想法?提前致谢。

更改视图模型中 Validate 方法的签名以接受 ChangesModel:

public void Validate(ChangesModel model)
{
    model.ChangeDate = DateTime.Now;
}

...并将您的 XAML 标记更改为:

<Button x:Name="Validate_Btn"
        cal:Message.Attach="[Event Click] = [Action Validate($this)]"
        cal:Action.TargetWithoutContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}"
        Visibility="...">Validate</Button>

要刷新 DataGrid 中的数据,您还需要为 ChangeDate 引发 PropertyChanged 事件 属性:

private DateTime _changeDate;
public DateTime ChangeDate
{
    get { return _changeDate; }
    set { _changeDate = value; NotifyPropertyChanged(); }
}