Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在codebehind中设置和获取文本框的updatesourcetrigger?_Wpf_Textbox_Updatesourcetrigger - Fatal编程技术网

Wpf 如何在codebehind中设置和获取文本框的updatesourcetrigger?

Wpf 如何在codebehind中设置和获取文本框的updatesourcetrigger?,wpf,textbox,updatesourcetrigger,Wpf,Textbox,Updatesourcetrigger,只是一个简短的问题: 在wpf中,如何设置和获取codebehind中文本框的UpdateSourceRigger? 谢谢 更新: 我遵循WPF的代码: var bndExp = BindingOperations.GetBindingExpression(this, TextBox.TextProperty); var myBinding = bndExp.ParentBinding; var updateSourceTr

只是一个简短的问题:
在wpf中,如何设置和获取codebehind中文本框的UpdateSourceRigger?
谢谢

更新:
我遵循WPF的代码:

        var bndExp = BindingOperations.GetBindingExpression(this, TextBox.TextProperty);

        var myBinding
          = bndExp.ParentBinding;

        var updateSourceTrigger = myBinding.UpdateSourceTrigger;
但我有个例外:

类型的未处理异常 中发生“System.Reflection.TargetInvocationException” PresentationFramework.dll其他信息:已发现异常 由调用的目标抛出


您所说的
文本框
更新资源记录器
是什么意思?您的意思是说
TextBox.TextProperty
绑定
UpdateSourceTrigger

例如,如果您有一个名为
myTextBox
TextBox
并将其
Text
属性绑定到某个源,那么您可以通过
GetBindingExpression()
调用轻松获取
updatesourcerigger
绑定
对象

   var bndExp
     = BindingOperations.GetBindingExpression(myTextBox, TextBox.Textproperty);

   var myBinding
     = bndExp.ParentBinding; 

   var updateSourceTrigger
     = myBinding.UpdateSourceTrigger;
但是,为已经使用过的绑定设置
更新资源记录器
是很棘手的。例如,在上述情况下,您将无法将
myBinding.UpdateSourceTrigger
设置为其他内容。当绑定对象已在使用时,这是不允许的

您可能需要深度克隆绑定对象,并为其设置新的
UpdateSourceTrigger
,然后将其分配回
文本框。
绑定
类不存在克隆。您可能需要为相同的目标编写自己的克隆代码

  var newBinding = Clone(myBinding); //// <--- You may have to code this function.
  newBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
  myTextBox.SetBinding(TextBox.TextProperty, newBinding);

请告诉我这些提示是否有用。

另一种实现方法是在TextBox加载的eventhandler中设置绑定。 下面是TextBox的xaml:

    <TextBox Grid.Row="0" 
             x:Name="txtName"
             Text="{Binding Name}"
             Loaded="TxtName_OnLoaded" />
正如您在上面的实现中所看到的,我们已经创建了一个具有新绑定路径的绑定对象。还将UpdateSourceTrigger分配给新创建的绑定对象

一个绑定可以有多个验证规则。我们将向其中添加验证规则。现在我们可以将绑定设置为textbox的TextProperty

优点:您可以从代码隐藏中将多个依赖项对象绑定到验证规则对象的属性,这在xaml中是不可能的。例如:

我使用它来验证TextChanged事件的输入,在这里我将输入与ApplicationViewModel中存储为公共ObservableCollection属性(绑定到网格)的项列表进行比较。验证规则的代码如下所示:

    public class UniqueValueValidator : ValidationRule
  {
    public string ErrorMessage { get; set; }
    public ApplicationViewModel ApplicationViewModel { get; set; }

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
      if (value == null)
        return null;

      var lws = ApplicationViewModel.Inputs.Where(s => s.Name.Equals(value.ToString())).FirstOrDefault();
      if (lws != null)
        return new ValidationResult(false, ErrorMessage);


      return new ValidationResult(true, null);
    }
  }
上面的代码接受输入并检查“输入”可观察集合中的可用性。如果值存在,则规则将给出false ValidationResult。通过这个实现,我检查运行时输入的唯一性


希望你们喜欢。

我认为这样做是正确的:

 Binding binding = new Binding();

 binding.Source = new CustomerViewModel();;
 binding.Path = new PropertyPath("Customer.Name");
 binding.Mode = BindingMode.TwoWay;
 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
 txtCustName.SetBinding(TextBox.TextProperty, binding);

嗨,AngelWPF,我遵循你的代码,但得到了例外。事实上,我想将所有textbox.TextProperty的绑定UpdateSourceTrigger设置为PropertyChanged,因此我想我可以在我的textbox基类(所有textbox都从一个基类继承)AngleWPF中进行设置,异常:抛出:“对象引用未设置为对象的实例。”(System.NullReferenceException)抛出System.NullReferenceException:“对象引用未设置为对象的实例。”您是否在代码中放入调试指针并查看哪个变量为null<如果在
TextBox
TextProperty
上没有进行现有绑定,则code>bndExp
将为空。我还希望
这个
是一个
文本框
对象。您要在自定义文本框中的哪个函数中检查此代码?我尝试在代码周围放置try catch,并意识到异常并非来自于此。我知道此答案已有7年历史,但是,如果它对任何人都有帮助,克隆选项对我有效,并且在的帮助下,可以在3行中克隆绑定。由于这些注释中没有太多空间,所以这是一种“丑陋”的单行方法来进行克隆:
公共静态绑定克隆(绑定)=>(绑定)XamlReader.Load(XmlReader.Create(新建StringReader(XamlWriter.Save(绑定))。这一行需要使用
System.Xml
System.Windows.Markup
,&
System.IO
    public class UniqueValueValidator : ValidationRule
  {
    public string ErrorMessage { get; set; }
    public ApplicationViewModel ApplicationViewModel { get; set; }

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
      if (value == null)
        return null;

      var lws = ApplicationViewModel.Inputs.Where(s => s.Name.Equals(value.ToString())).FirstOrDefault();
      if (lws != null)
        return new ValidationResult(false, ErrorMessage);


      return new ValidationResult(true, null);
    }
  }
 Binding binding = new Binding();

 binding.Source = new CustomerViewModel();;
 binding.Path = new PropertyPath("Customer.Name");
 binding.Mode = BindingMode.TwoWay;
 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
 txtCustName.SetBinding(TextBox.TextProperty, binding);