【问题标题】:Add ValidationRule in XAML from code behind从后面的代码中添加 XAML 中的 ValidationRule
【发布时间】:2015-06-19 20:20:56
【问题描述】:

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

我在 google 上找到了这个,但我得到了以下结果(删除了不相关的属性以提高可读性:

<TextBox Text="{Binding ValidatesOnDataErrors=True, 
                Path=Model.txt0.Value, 
                UpdateSourceTrigger=PropertyChanged}" >

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

【问题讨论】:

  • ValidatesOnDataErrors 正在处理 IDataErrorInfo 而不是 ValidationRules 试试这个
  • 谢谢,但我不能使用 IDataErrorInfo,因为在我的情况下,我需要将验证保存在 xaml 中。我在用户选择控件时动态创建控件,他应该能够选择预定义的验证,然后保存网格以便稍后再次加载。
  • 我不明白为什么你不能在这种情况下使用数据绑定,而实际上它是为这种情况而设计的,如果是这样,你也可以使用 IDataErrorInfo。创建一个表示选项的 VM,然后在 XAML 中将其绑定到它,并为显示网格的 VM 指定一个 DataTemplate。 WPF中常用的
  • 我不确定我是否理解你。让我再告诉你一次。我需要动态创建 XAML。用户根据自己的需要拖放控件。然后选择验证。还支持版本控制,如果有新版本,相同的 XAML 可能会有不同的验证。我不想将验证规则放在数据库中,因为我会为用户进行的每个 EDIT 设置不同的行,这会及时在数据库中存在大量数据。将 ValidationRules 放入 XAML 将减小数据库大小并提高速度。
  • 此问题中的代码看起来与另一位用户两小时前提出的代码相同。 stackoverflow.com/questions/30933502

标签: c# wpf xaml code-behind validationrules


【解决方案1】:

您应该检查您的视图模型。您的示例适用于以下测试用例。

<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;
    }

和视图模型

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; }
}

【讨论】:

  • 感谢您的详细回答,但我真正需要的是将 XAML 保存在数据库中。我有这个: THE XAML HERE 我提取动态生成的 XAML 并将其保存为数据库中的 varchar。此方法不给我 XAML。你知道我如何获得像我需要的那样生成的 xaml(问题)
  • 我会犹豫对现有可视化树中的任何 XAML 进行逆向工程。为什么不直接连接字符串?
  • 我使用某个用户在代码项目中提供的类对 XAML 进行逆向工程。这些课程工作得很好而且很精确。问题是 ValidationRules 绑定。是否有另一种方法可以将 ValidationRule 绑定到 XAML,以便我在上面获得所需的输出?
  • 代码项目文章:codeproject.com/Articles/569753/… 有演示链接。下载它,点击添加行,拖放一些东西,然后点击序列化。这得到了 XAML。我似乎无法从后面的代码创建 xaml(我正在谈论的绑定)。获取 xaml 就完成了。再次感谢您提供帮助。
  • 我将否决这个问题,因为它与您的问题无关。您遇到了 AlexDov 编写的 XAMLWriter 类的问题。您需要单步执行代码,找出问题所在,并使其成为 StackOverflow 上更小、更集中的问题。提示:XAMLWriter 可能没有遍历 Binding 的 Validations 集合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 2014-01-18
  • 2011-01-01
  • 1970-01-01
相关资源
最近更新 更多