gpt4 book ai didi

c# - 在ViewModel中修改后,String属性在View中不会更新

转载 作者:行者123 更新时间:2023-12-03 10:26:52 36 4
gpt4 key购买 nike

我有一个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,因为该值不会更改。您最好改用其他属性来控制错误的可见性。

关于c# - 在ViewModel中修改后,String属性在View中不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39208356/

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