【问题标题】:IDataErrorInfo validation not triggered while using data template in UI在 UI 中使用数据模板时未触发 IDataErrorInfo 验证
【发布时间】:2012-07-09 12:31:53
【问题描述】:

我正在尝试根据 ViewModel 公开的属性构建我的应用程序设置页面。我正在使用 .Net 4.0 和 MVVM。 ViewModel 公开了“一组设置值”的单个集合。组表示相互依赖并属于域的逻辑组的属性。视图中的设置页面是使用 DataTemplate 创建的,如下所示:-

<DataTemplate x:Key="contentSettingGroup1">
  <TextBlock Text="{Binding Field1Description}" />
  <TextBox Text="{Binding Field1Value, Mode=TwoWay}" Grid.Column="2" />

  <TextBlock Text="{Binding Field2Description}"  />
  <TextBox Text="{Binding Field2Value, Mode=TwoWay}" Grid.Column="6" />
</DataTemplate>

<DataTemplate DataType="{x:Type vm:SettingGroup1}">
  <HeaderedContentControl Header="{Binding}" HeaderTemplate="{StaticResource titleArea}" Content="{Binding}" ContentTemplate="{StaticResource contentSettingGroup1}" />
</DataTemplate>

然后我在 ViewModel 模块中有一个类来表示“设置组”,如下所示:

public class SettingGroup1 : INotifyPropertyChanged, IDataErrorInfo
{
    public double Field1value { get; private set; }
    public double Field2value { get; private set; }

    private double mField1;
    public double Field1value
    {
        get { return mField1; }
        set
        {
            if (mField1 != value)
            {
                mField1 = value;
                RaisePropertyChanged(() => Field1value);
            }
        }
    }

    private double mField2;
    public double Field2value
    {
        get { return mField2; }
        set
        {
            if (mField2 != value)
            {
                mField2 = value;
                RaisePropertyChanged(() => Field2value);
            }
        }
    }

    public string Error
    {
        get { return null; }
    }

    public string this[string property]
    {
        get
        {
            string errorMsg = null;
            switch (property)
            {
                case "Field1value":
                    if (Field1value < 0.0)
                    {
                        errorMsg = "The entered value of Field1 is invalid !";
                    }
                    if (Field1value < Field2value)
                    {
                        errorMsg = "The Field1 should be greater than Field2 !";
                    }
                    break;
            }
            return errorMsg;
        }
    }
}

最后,viewModel 公开了这样一组设置的集合:

public ObservableCollection<object> Settings
        {
            get
            {
                var pageContents = new ObservableCollection<object>();
                var group1 = new SettingGroup1();
                group1.Field1.Description = "Description value 1";
                group1.Field1.Value = mValue1;
                group1.Field2.Description = "Description value 2";
                group1.Field2.Value = mValue2;
                pageContents.Add(group1);

                // add other groups of controls
                // ...
                return pageContents;
            }
        }

问题:调用了属性设置器,但无论何时更改 UI 值都不会调用数据验证。我也尝试将 IDataErrorInfo 实现放在 ViewModel 类中,但没有一个有效。我必须使用一组设置,因为这些应用程序设置用于许多项目,我们不希望每个应用程序都有重复的 XAML。 注意: viewmodel 不会暴露 UI 绑定到的属性,例如Field1Value 但暴露了一个封装的对象。

【问题讨论】:

    标签: wpf datatemplate idataerrorinfo


    【解决方案1】:

    你并没有告诉你的视图你绑定的属性需要被验证。在绑定中使用“ValidatesOnDataErrors = true”。

    <TextBox Text="{Binding Field1Value, Mode=TwoWay, ValidatesOnDataErrors=True}" Grid.Column="2" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多