从代码隐藏中添加XAML中的ValidationRule

时间:2015-06-19 11:19:19

标签: c# wpf xaml code-behind validationrules

我正在尝试从后面的代码向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}" >

如何获得我需要的结果(第一个代码)?感谢

1 个答案:

答案 0 :(得分:2)

您应该检查您的viewmodel。您的样本使用以下测试用例。

<TextBox x:Name="Txt0">

验证

using System.Globalization;
using System.Windows.Controls;

namespace WpfApplication2
{
    public class RequiredValidate : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            return value != null ? ValidationResult.ValidResult : new ValidationResult(false, "Value required");
        }
    }
}

背后的代码

    private void InitializeValidation()
    {

        FrameworkElement SelectedObject = Txt0;
        DependencyProperty property =
            GetDependencyPropertyByName(SelectedObject, "TextProperty");
        Binding binding = new Binding("Model.Txt0");
        binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        binding.ValidatesOnDataErrors = true;
        RequiredValidate role = new RequiredValidate();
        binding.ValidationRules.Add(role);
        SelectedObject.SetBinding(property, binding);
    }

    public static DependencyProperty GetDependencyPropertyByName(DependencyObject dependencyObject, string dpName)
    {
        return GetDependencyPropertyByName(dependencyObject.GetType(), dpName);
    }

    public static DependencyProperty GetDependencyPropertyByName(Type dependencyObjectType, string dpName)
    {
        DependencyProperty dp = null;

        var fieldInfo = dependencyObjectType.GetField(dpName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
        if (fieldInfo != null)
        {
            dp = fieldInfo.GetValue(null) as DependencyProperty;
        }

        return dp;
    }

和ViewModels

public class MainWindowViewModel
{
    public MainWindowViewModel()
    {
        Model = new Model();
    }

    public Model Model { get; set; }
}

public class Model
{
    public Model()
    {
        Txt0 = 42;
        Txt1 = 99;
    }

    public int? Txt0 { get; set; }
    public int? Txt1 { get; set; }
}