首页 文章

如何在字符串索引器数据绑定上实现IDataErrorInfo?

提问于
浏览
4

使用xaml(注意字典条目属性[Welcome]上的绑定):

<Grid x:Name="LayoutRoot">
        <StackPanel>
            <TextBlock FontSize="36"
                   FontWeight="Bold"
                   Foreground="Purple"
                   Text="{Binding Attributes[Welcome]}"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   TextWrapping="Wrap" />
            <TextBox Text="{Binding Attributes[Welcome],Mode=TwoWay, ValidatesOnDataErrors=True}"></TextBox>
            <TextBox Text="{Binding Attributes[Welcome],Mode=TwoWay, ValidatesOnDataErrors=True}"></TextBox>
            <TextBox Text="{Binding Test, Mode=TwoWay, ValidatesOnDataErrors=True}"></TextBox>
            <TextBox Text="{Binding Test, Mode=TwoWay, ValidatesOnDataErrors=True}"></TextBox>
        </StackPanel>
    </Grid>

当视图模型将IDataErrorInfo实现为:

public string Error
        {
            get { return ""; }
        }

        public string this[string columnName]
        {
            get { 
                return "Compulsory Error"; 
            }
        }

只传递columnName == "Test" . 因此我得到以下申请:
enter image description here

如何验证为“属性词典”设置的值?

2 回答

  • 0

    我想我需要在Dictionary上实现IDataErrorInfo而不是包含字典的viewmodel . 但由于IDataErrorInfo成员与IDicitonary发生冲突 . 我最终实现了INotifyDataErrorInfo .

  • 0

    而不是使用字典,更多的“MVVMish”方式是为要在列表中显示的项目创建一个简单的ViewModel . 然后将它们添加到列表(而不是字典)并绑定到这些项目 . 然后,您可以在这些ViewModel上实现IDataErrorInfo(以及任何其他自定义逻辑或您需要的任何其他内容) .

相关问题