c# - 如果存在验证错误,也要更新 ViewModel 中的属性

标签 c# wpf validation xaml mvvm

在我的应用程序中,我对 DataGridTemplateColumn 有以下定义:

<DataGridTemplateColumn Header="Placeholder-Name" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox IsEditable="True" VerticalAlignment="Center" Margin="2"
                      ItemsSource="{Binding DataContext.AvailablePlaceholders, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
                      DisplayMemberPath="PlaceholderName"
                      Validation.ErrorTemplate="{StaticResource ComboBoxErrorTemplate}">
                <ComboBox.Text>
                    <Binding Path="PlaceholderName" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <local:PlaceholderValidationRule>
                                <local:PlaceholderValidationRule.PlaceholderValidationRuleParamData>
                                    <local:PlaceholderValidationRuleParamData 
                                        UsedPlaceholders="{Binding Source={StaticResource proxy}, Path=Data.PlaceholderItems}"
                                        AvailablePlaceholders="{Binding Source={StaticResource proxy}, Path=Data.AvailablePlaceholders}"/>
                                </local:PlaceholderValidationRule.PlaceholderValidationRuleParamData>
                            </local:PlaceholderValidationRule>
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.Text>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

一切正常,但我刚刚意识到,如果 PlaceholderValidationRule 报告无效的结果,我的 ViewModel 中的属性 PlaceholderName 是 ComboBox - 文本必然不会被更新。

如果存在验证错误,是否有任何方法可以从 View 更新 ViewModel 中的属性?

最佳答案

Is there any way to update the property in the ViewModel from the View also if there are Validation-Errors?



设置ValidationStep ValidationRule 的属性(property)至UpdatedValue :
<local:PlaceholderValidationRule ValidationStep="UpdatedValue">

这将导致验证规则在源属性更新后运行。

关于c# - 如果存在验证错误,也要更新 ViewModel 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867430/

相关文章:

c# - HttpContent.ReadAsStringAsync 导致请求挂起(或其他奇怪的行为)

c# - 使用最小起订量模拟 IEnumerable<T>

c# - Entity Framework 同一个表的多个外键

javascript - 数字类型输入的onkeyup vs onchange

php - 表单验证以及数据库错误

java - 使用本地过程将数据插入数据库或使用最近的框架(如 spring 或 Entity Framework )将数据插入数据库之间有什么区别吗?

wpf - 如何在启用或禁用按钮时更改自定义按钮中标签的前景色?

c# - WPF:设计器未能添加第三方控件。这导致设计器重新启动

javascript - 如何让正则表达式适用于 mm/dd/yyyy 和 yyyy-mm-dd 格式

c# - 如何使用DataGrid和MVVM添加行