繁体   English   中英

使用 Caliburn Micro 更新 WPF 中的 DataGrid

[英]Updating DataGrid in WPF with Caliburn Micro

我正在使用 Caliburn Micro 进行 WPF 项目。 在这个应用程序中,我有一个DataGrid ,我使用 Dapper 从 SQL Server 数据库中填充数据。 请考虑以下代码片段:

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

概览Viewmodel.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
        }
    }
}

概览视图.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 ButtonCheckBox的布尔值设置为 true, ValidationDate设置为 now 并且DataGrid更新。 然后我将触发一个存储过程来更新数据库表。 另请注意, 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(); }
}

暂无
暂无

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

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