How to implement IDataErrorInfo on string indexers databinding?

With xaml (notice the binding on dictionary entry Attributes[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>
When the view model implements IDataErrorInfo as:


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

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


Only columnName == "Test" is ever passed. And therefore I get the following application:
How can I validate the values being set for the Attributes Dictionary?


I figured that I needed to implement IDataErrorInfo on the Dictionary rather than the viewmodel containing the dictionary. But since IDataErrorInfo member's conflict with IDicitonary. I ended up implementing INotifyDataErrorInfo.


Instead of using a Dictionary the more "MVVMish" way would be to create a simple ViewModel for the items you are going to display in your list. Then add them to a list (instead of a dictionary) and bind to those items. Then you can implement IDataErrorInfo on those ViewModels (along with any other custom logic or anything else you need).

链接地址: http://www.djcxy.com/p/44622.html

上一篇: 如何绑定父级中的子级用户控件的数据上下文

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