我有一个WPF窗口,其中显示基于一系列日期的事件列表。我还有一个字符串属性,如果用户选择了不正确的日期范围,我想在其中显示错误消息。 ("从"日期大于"到"日期。)
在更改的日期控制值上,我设置了一个刷新datagrid中事件的命令。在此之前,我保证日期范围有效。如果没有,我在标签中显示错误消息。
问题在于,即使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
}
}
答案 0 :(得分:2)
首先,您要设置属性的后场两次。一旦手动,然后再使用SetProperty。 SetProperty实际上会检查值是否相同,它们不会设置属性或调用INPC。所以你基本上是设置它,然后尝试再次设置它,这不会发生。
你的财产应该这样定义:
private string _dateRangeError;
public string DateRangeError
{
get { return _dateRangeError; }
set { SetProperty(ref _dateRangeError, value); }
}
然后您遇到错误消息相同的问题。因此,永远不会调用INPC,因为价值永远不会改变。您可能最好使用不同的属性来控制错误的可见性。