gpt4 book ai didi

c# - 使用Caliburn Micro在WPF中更新DataGrid

转载 作者:行者123 更新时间:2023-12-03 10:31:37 25 4
gpt4 key购买 nike

我正在使用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
}
}
}

概述View.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的 bool 值设置为true, ValidationDate设置为now,并且 DataGrid更新。然后,我将触发一个存储过程来更新数据库表。另请注意,只有在选中 Button时,才能看到 CheckBox。因此,我只想知道如何在 Validated方法 ValidationDate中访问 ViewModel属性和 Validate()。另外,更改 DataGridValidated的值后,如何更新 ValidationDate,以便如果我打开另一个 ContentControl,这些值不会重置?
有人有想法么?提前致谢。

最佳答案

在 View 模型中更改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中的数据,您还需要引发 PropertyChanged属性的 ChangeDate事件:
private DateTime _changeDate;
public DateTime ChangeDate
{
get { return _changeDate; }
set { _changeDate = value; NotifyPropertyChanged(); }
}

关于c# - 使用Caliburn Micro在WPF中更新DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60090636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com