在ViewModel中修改字符串属性时,视图中不会更新
本文关键字:视图 更新 属性 ViewModel 修改 字符串 | 更新日期: 2023-09-27 18:04:37
我有一个WPF窗口,其中显示基于日期范围的事件列表。我还有一个字符串属性,如果用户选择了不正确的日期范围,我希望在其中显示错误消息。("从"日期大于"到"日期)
在更改日期控件值时,设置一个命令来刷新数据网格中的事件。在此之前,验证日期范围是否有效。如果不正确,则在标签中显示错误信息。
问题是,即使ViewModel将属性设置为错误消息的文本,View也不会使用相应的值进行更新。
我做错了什么?
下面是我的一些代码示例。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:prism="http://prismlibrary.com/"
xmlns:igDP="http://infragistics.com/DataPresenter"
xmlns:igEditors="http://infragistics.com/Editors"
x:Class="ADM.Module.Event.Views.EventLogView"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="140"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Content="{Binding Header, Mode=OneTime}" FontSize="25" FontWeight="Bold" Grid.ColumnSpan="5"/>
<Label Content="Select a date range : " Grid.Row="1" />
<igEditors:XamDateTimeEditor HorizontalAlignment="Left" Margin="2,2,2,2" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top"
Format="yyyy-MM-dd HH:mm:ss" Mask="yyyy-mm-dd hh:mm:ss" Value="{Binding DateFrom}" Theme="Office2010Blue" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding RefreshEventLogCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</igEditors:XamDateTimeEditor>
<Label Content=" to " Grid.Row="1" Grid.Column="2" />
<igEditors:XamDateTimeEditor HorizontalAlignment="Left" Margin="2,2,2,2" Grid.Row="1" Grid.Column="3" VerticalAlignment="Top"
Format="yyyy-MM-dd HH:mm:ss" Mask="yyyy-mm-dd hh:mm:ss" Value="{Binding DateTo}" Theme="Office2010Blue" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding RefreshEventLogCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</igEditors:XamDateTimeEditor>
<Label Content="{Binding DateRangeError}" Foreground="Red" Grid.Row="2" Grid.ColumnSpan="5" />
<igDP:XamDataGrid HorizontalAlignment="Stretch" Margin="2,2,2,2" Grid.Row="3" Grid.ColumnSpan="5" VerticalAlignment="Stretch"
DataSource="{Binding EventLogRecords}" Theme="Office2010Blue">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AllowDelete="False" HighlightAlternateRecords="True" AllowAddNew="False" AutoArrangeCells="LeftToRight"
ResizingMode="Immediate" SelectionTypeRecord="Single" AutoGenerateFields="False" />
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings AllowRecordFiltering="True" />
</igDP:XamDataGrid.FieldSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:Field Name="EventDate" Label="Date" AllowEdit="False" Format="yyyy-MM-dd H:mm:ss" >
<igDP:Field.Settings>
<igDP:FieldSettings AllowRecordFiltering="False" />
</igDP:Field.Settings>
</igDP:Field>
<igDP:UnboundField Name="Severities" Label="Severity" BindingPath="Severities.SeverityText" AllowEdit="False" />
<igDP:UnboundField Name="LogTypes" Label="Type" BindingPath="LogTypes.LogTypeName" AllowEdit="False" />
<igDP:Field Name="EventMsg" Label="Message" AllowEdit="False" >
<igDP:Field.Settings>
<igDP:FieldSettings FilterOperandUIType="TextBox" FilterOperatorDefaultValue="Contains" />
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
using Prism.Commands;
using ADM.DataModel;
using ADM.Infrastructure.BaseClass;
using ADM.Module.Event.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace ADM.Module.Event.ViewModels
{
public class EventLogViewModel : TabViewModelBase, IEventLogViewModel
{
private EventLogModel _model;
public EventLogViewModel()
{
Header = "Event Log";
var now = DateTime.Now;
DateFrom = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
DateTo = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
_model = new EventLogModel();
EventLogRecords = _model.GetEventLogsByDate(DateFrom, DateTo);
RefreshEventLogCommand = new DelegateCommand(RefreshEventLog);
}
#region Properties
private DateTime _dateFrom;
public DateTime DateFrom
{
get { return _dateFrom; }
set
{
_dateFrom = value;
SetProperty(ref _dateFrom, value);
}
}
private DateTime _dateTo;
public DateTime DateTo
{
get { return _dateTo; }
set
{
_dateTo = value;
SetProperty(ref _dateTo, value);
}
}
private string _dateRangeError;
public string DateRangeError
{
get { return _dateRangeError; }
set
{
_dateRangeError = value;
SetProperty(ref _dateRangeError, value);
}
}
private ObservableCollection<EventLogs> _eventLogRecords;
public ObservableCollection<EventLogs> EventLogRecords
{
get {
return _eventLogRecords;
}
private set {
_eventLogRecords = value;
SetProperty(ref _eventLogRecords, value);
}
}
#endregion Properties
#region Commands
public ICommand RefreshEventLogCommand { get; private set; }
public void RefreshEventLog()
{
if (ValidateDateRange(DateFrom, DateTo))
{
EventLogRecords.Clear();
EventLogRecords.AddRange(_model.GetEventLogsByDate(DateFrom, DateTo));
}
}
private bool ValidateDateRange(DateTime fromDate, DateTime toDate)
{
if (DateFrom > DateTo)
{
DateRangeError = "The From Date need to be set to before the To Date.";
return false;
}
return true;
}
#endregion Commands
}
}
using Prism;
using Prism.Mvvm;
using ADM.Infrastructure.DockManager;
using System;
namespace ADM.Infrastructure.BaseClass
{
public class TabViewModelBase : BindableBase, IActiveAware, IDockAware
{
public TabViewModelBase()
{
}
#region IActiveAware
bool _isActive;
public bool IsActive
{
get { return _isActive; }
set
{
_isActive = value;
SetProperty(ref _isActive, value);
}
}
public event EventHandler IsActiveChanged;
#endregion //IActiveAware
#region IDockAware
private string _header;
public string Header
{
get { return _header; }
set
{
_header = value;
SetProperty(ref _header, value);
}
}
#endregion //IDockAware
}
}
首先,您设置了两次属性的后台。一次手动,然后再次使用SetProperty。SetProperty实际上检查值是否相同,如果是,它不会设置属性或调用INPC。因此,您实际上是在设置它,然后尝试再次设置它,这将不会发生。
你的属性应该这样定义:
private string _dateRangeError;
public string DateRangeError
{
get { return _dateRangeError; }
set { SetProperty(ref _dateRangeError, value); }
}
那么你的错误信息是相同的问题。同样,INPC永远不会被调用,因为它的值永远不会改变。您最好使用不同的属性来控制错误的可见性。