在我的应用程序中,我对 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/