gpt4 book ai didi

c# - IDataErrorInfo 错误消息触发,但未显示任何消息

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:00 26 4
gpt4 key购买 nike

各位程序员大家好。我在使用 IDataErrorInfo 的 Silverlight MVVM 模式上遇到了这个奇怪的问题。这是我在 StackOverflow 提出的第一个问题,所以我希望我能正确解释它。这是交易:

我有一个巨大的 Job Register ChildWindow,它有一些控件,只有在选中名为 HasPrevision 的复选框时才应启用这些控件。这个想法是:如果作业有一个 Prevision,那么所有与 Prevision 相关的控件都是 Enabled,否则,它们都是 Disabled。现在,如果有预览,用户必须在 PrevisionType 组合框中选择一个 Prevision Type。因此,如果选中 HasPrevision 复选框,则必须选中 PrevisionType,但如果未选中 HasPrevision 复选框,则可以不选中 PrevisionType 组合框。

这是这些控件的 XAML(我不会在这里显示所有的 xaml,因为它相当大,但我相信只要显示这些行就可以解决问题):


<!-- The HasPrevision CheckBox -->
<CheckBox Grid.Column="1"
Grid.Row="3"
x:Name="cbxHasPrevision"
IsChecked="{Binding SelectedJob.HasPrevision, Mode=TwoWay}"
Margin="0,20,2,0" />

<!-- The PrevisionType ComboBox -->
<telerik:RadComboBox Grid.Column="1"
Grid.Row="4"
x:Name="cmbPrevisionType"
IsEnabled="{Binding HasPrevision, Converter={StaticResource ControlesCadastroProcessoConverter}, ConverterParameter=IsEnabled}"
ItemsSource="{Binding PrevisionTypeList, Mode=OneWay}"
SelectedItem="{Binding SelectedJob.PrevisionType , Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, ValidatesOnNotifyDataErrors=True}"
Margin="0,4,48,4"
Height="22"
DisplayMemberPath="DsPrevisionType"/>

因此,如您所见,HasPrevision 和 PrevisionType 属性绑定(bind)到我的 ViewModel 中名为 SelectedJob 的属性。 SelectedJob 是一个作业对象。我想要做的就是使用我的作业类中的 IDataErrorInfo 接口(interface)来确定是否选择了 PrevisionType。现在,如果 HasPrevision 属性为“true”,我只能需要一个 PrevisionType,因此我的作业类具有用于 IDataErrorInfo 的代码:


public string this[string columnName]
{
get
{
switch (columnName)
{
case "PrevisionType":
if (HasPrevision && (PrevisionType== null))
return "Select a PrevisionType";
break;
}
return null;
}

这对我来说听起来很合乎逻辑。当我调试代码时,它确实有效:开关识别 columnName,它进入案例“PrevisionType”,它实际上运行返回错误消息的 If 语句内的返回代码,而不是默认的“return null”子句。但是错误消息不显示错误!更糟糕的是:组合框不会自动进行事件验证。单击“保存”按钮后,我检查错误并将 PrevisionType 组合框设置为 InvalidFocused 状态。 comboBox 确实在其周围出现红色边框,表明它处于错误状态,但错误消息为空白。当我将鼠标悬停在控件上时,会出现一个空的红色小框,上面什么也没有。

甚至比这更奇怪:如果我从 IF 语句中删除“HasPrevision”检查,保留代码如下,则会显示消息并自动验证控件!!!


public string this[string columnName]
{
get
{
switch (columnName)
{
case "PrevisionType":
if (PrevisionType== null)
return "Select a PrevisionType";
break;
}
return null;
}
}

我能想到的一切是,因为我试图根据另一个属性值验证一个属性,在集合和获取中间的某个地方,Silverlight 环境变得“困惑”了吗?我不知道,我一直在尝试为此找到解决方案 2 天,但没有成功。以下是我认为相关的其他一些代码。

我的工作类中的属性:


    private bool _HasPrevision;
[DataMember]
public bool HasPrevision
{
get { return _HasPrevision; }
set
{
_HasPrevision= value;
RaisePropertyChanged("HasPrevision");
}
}


private PrevisionType.PrevisionType _PrevisionType { get; set; }
[DataMember]
public PrevisionType.PrevisionType PrevisionType
{
get { return _PrevisionType; }
set
{
_PrevisionType = value;
RaisePropertyChanged("PrevisionType");
}
}

我的 ViewModel 中的 SelectedJob 属性是:


public const string SelectedJobPropertyName = "SelectedJob";
private Objects.Register.Job.Job _SelectedJob = null;
public Objects.Register.Job.Job SelectedJob
{
get
{
return _SelectedJob;
}

set
{
if (_SelectedJob == value)
return;

var oldValue = _SelectedJob;
_SelectedJob = value;
RaisePropertyChanged(SelectedJobPropertyName , oldValue, value, true);
}
}

我的保存按钮代码是:


[...]

if(SelectedJob["PrevisionType"] != null)
{
VisualStateManager.GoToState(cmbPrevisionTypeControl, "InvalidFocused", true);
return;
}

[...]

提前致谢,如果我在解释这个问题时有点困惑,我很抱歉。

编辑 1:我已经在这里阅读了这些问题,但它们对我的帮助不大:

IDataErrorInfo - not seeing any error message even though one gets picked up

Silverlight IDataErrorInfo message does not show in custom control's textbox

最佳答案

我正在以类似的方式使用 Telerik RadComboBox,并且在显示错误消息时没有遇到任何问题。这是我的 XAML 供您引用,希望对您有所帮助...

 <TextBlock Text="Select a User to Assign the Current Step To:" Grid.Row="3" Visibility="{Binding ShowAssigneeUser}"/>
<telerik:RadComboBox x:Name="cmbAllActiveCrmUsers"
IsEditable="True"
Grid.Row="4"
telerik:TextSearch.TextPath="FullName"
Text="{Binding FullName, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
VerticalAlignment="Center"
Visibility="{Binding ShowAssigneeUser}"
ItemsSource="{Binding AllActiveCrmUsersExceptCurrentUser}"
SelectedIndex="{Binding DefaultAssigneeUserIndex, Mode=TwoWay}"
ItemTemplate="{StaticResource ComboBoxMultilineTemplate}"
Style="{StaticResource ComboBoxStyle}"
Command="{Binding AssignUserChangeCommand}"
CommandParameter="{Binding SelectedIndex, ElementName=cmbAllActiveCrmUsers}"
>
<telerik:RadComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</telerik:RadComboBox.ItemsPanel>
</telerik:RadComboBox>

关于c# - IDataErrorInfo 错误消息触发,但未显示任何消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12249782/

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