[英]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 Button
, CheckBox
的布爾值設置為 true, ValidationDate
設置為 now 並且DataGrid
更新。 然后我將觸發一個存儲過程來更新數據庫表。 另請注意, Button
僅在CheckBox
被選中時才可見。 所以我只想知道如何訪問ViewModel
方法Validate()
的Validated
屬性和ValidationDate
。 另外,在更改Validated
和ValidationDate
的值后,我將如何更新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.