我正在尝试从后面的代码向 XAML 添加一个 ValidationRule,并且需要这样做:
<TextBox.Text>
<Binding Path="Model.txt1.Value" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<localVal:RequiredValidate />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
到目前为止,我已经尝试过:
FrameworkElement SelectedObject = fe_dragged_control;
DependencyProperty property =
ControlBindingExtensions.GetDependencyPropertyFromName("Text", SelectedObject.GetType());
Binding binding = new Binding("Model." + SelectedObject.Name + ".Value");
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.ValidatesOnDataErrors = true;
RequiredValidate role = new RequiredValidate();
binding.ValidationRules.Add(role);
SelectedObject.SetBinding(property, binding);
我在谷歌上找到了这个,但我得到了以下结果(删除了不相关的属性以提高可读性:
<TextBox Text="{Binding ValidatesOnDataErrors=True,
Path=Model.txt0.Value,
UpdateSourceTrigger=PropertyChanged}" >
如何获得我需要的结果(第一个代码)?谢谢