Я работаю над проектом WPF с использованием Caliburn Micro. В этом приложении у меня есть DataGrid
, который я заполняю данными из базы данных SQL Server, используя Dapper. Обратите внимание на следующие фрагменты кода:
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>
То, что я хотел бы выполнить sh, заключается в следующем: когда пользователь нажимает кнопку Validate Button
, логическое значение для CheckBox
устанавливается на true, ValidationDate
установлен на сейчас, а DataGrid
обновлен. Затем я запускаю хранимую процедуру для обновления таблицы базы данных. Также обратите внимание, что Button
отображается только в том случае, если отмечен CheckBox
. Итак, все, что я хочу знать, это как получить доступ к свойству Validated
и ValidationDate
в методе ViewModel
Validate()
. Кроме того, как мне обновить DataGrid
после изменения значений для Validated
и ValidationDate
, чтобы при открытии другого ContentControl
значения не сбрасывались? У кого-нибудь есть идеи? Заранее спасибо.