【问题标题】:IDataErrorInfo error message triggers, but no message is shownIDataErrorInfo 错误消息触发,但未显示任何消息
【发布时间】:2012-08-28 07:18:16
【问题描述】:

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

我有一个巨大的 Job Register ChildWindow,它有一些控件,只有在选中名为 HasPrevision 的复选框时才应启用这些控件。这个想法是:如果作业有一个Prevision,那么所有与prevision相关的控件都被启用,否则,它们被禁用。 现在,如果有预置,用户必须在 PrevisionType ComboBox 中选择预置类型。因此,如果选中 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 属性绑定到我的 ViewModel 中名为 SelectedJob 的属性。 SelectedJob 是一个 Job 对象。我要做的就是在我的 Job Class 中使用 IDataErrorInfo 接口来识别是否选择了 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;
        }

}

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

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


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

我能想到的一切是,由于我试图根据另一个属性值验证一个属性,所以在 set 和 get 中间的某个地方,Silverlight 环境变得“混乱”了吗?我不知道,我已经尝试了 2 天来解决这个问题,但没有成功。 以下是我认为相关的其他一些代码。

我的 Job Class 中的属性:


    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

【问题讨论】:

    标签: c# silverlight xaml mvvm idataerrorinfo


    【解决方案1】:

    我正在以类似的方式使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多