WPF:TextBox文本未更新 [英] WPF: TextBox Text is not updating

查看:393
本文介绍了WPF:TextBox文本未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DataTemplate 中使用了一个用户控件,该 UserControl 包含一个 TextBox 与我的 Value 属性(声明为 DependencyProperty )绑定UserControl 。在数据模板中,我将此 Value 属性绑定到我的实际属性,即 Name (也是 DependencyProperty )。它工作正常,我在加载时为 Name 属性分配了一个值,我的 TextBox 显示了它,我将值从更改了TextBox ,它还会更新我的 Name 属性。
现在问题出现了,当我在依赖属性中添加 PropertyChangedEventHandler 时,我检查该值是否有效,如果有效则不执行任何操作,并在无效。如果值无效,当我为它分配旧值时,属性会更新,但不会在我的 UserControl TextBox 中显示更新的值。谁能告诉我为什么?

I have a user control that i am using inside a DataTemplate, this UserControl contains a TextBox which is binded with Value property(declared as a DependencyProperty) of my UserControl. In data template I bind this Value property with my actual property say Name (also a DependencyProperty). It works properly, I assign a value to Name property while loading and my TextBox displays it, i change the value from TextBox and it updates my Name property also. Now the problem comes while I add a PropertyChangedEventHandler in the dependency property, I check for the value if it is valid, do nothing if valid and assign old value if not valid. In case of value not valid, when I assign it that old value, property updates but it is not displaying updated value in my TextBox of UserControl. Can anyone tell me why?

XAML 我的 UserControl

<UserControl x:Name="usercontrol">
   <StackPanel>
      <TextBlock ......./>
      <TextBox Binding={Binding ElementName=usercontrol, Path=Value, Mode=TwoWay}/>
   </StackPanel>
</UserControl>

Value 后面的代码中是 DependencyProperty

我正在我的 DataTemplate 中使用它:

<HierarchicalDataTemplate DataType="{x:Type myLib:myClass}" ItemsSource="{Binding Children}">
    <Expander Header="{Binding}">
        <WrapPanel>
            <edproperty:TextPropertyEditor Caption="Name" Value="{Binding Name, Mode=TwoWay}"/>
            <edproperty:TextPropertyEditor .................../>
            <edproperty:TextPropertyEditor .................../>
            <edproperty:TextPropertyEditor ...................../>
        </WrapPanel>
    </Expander>
</HierarchicalDataTemplate>

我在 TreeView 中使用的此模板。在 TreeView 内,我在 TextBox 中输入值,该值会更新<$>的 Value 属性。 c $ c> UserControl ,依次更新 myClass Name 属性,我分配了一个 PropertyChangedCallback 到我的 myClass 中的 Name 属性,执行一些验证并重新分配 OldValue 如果验证失败。它反过来也更新了 Value 属性,但是我仍然看到 TextBox 具有与我之前输入的值相同的值,而不是更新的值。

This template I am using in a TreeView. Inside TreeView I enter value in the TextBox which updates the Value property of my UserControl, that in turn updates Name property of myClass I am assigning a PropertyChangedCallback to my Name Property in myClass, performing some validation and reassigning the OldValue if the validation fails. It in turn updates Value property also, but i am still seeing that the TextBox has the same value that i entered earlier not the updated one.

public string Name
    {
        get
        {
            return (string)GetValue(NameProperty);
        }
        set
        {
            SetValue(NameProperty, value);
        }
    }

    // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NameProperty =
        DependencyProperty.Register("Name", typeof(string), typeof(myClass), new UIPropertyMetadata(null, OnNameChanged));

    protected static void OnNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        if(checkRequired && !IsValid(e.NewValue.ToString()))
        {
            checkRequired=false;
            (sender as myClass).Name = args.OldValue.ToString();
        }
    }

    private static bool IsValid(string name)
    {
         .............some logic here
    }


推荐答案

经过大量搜索,我找到了解决问题的方法

After days of looking around and a lot of search i found the solution of my problem

完成所有验证并分配旧值并更新Dependecy属性后,我需要校准UpdateTarget()方法,来更新我的TextBox中的值。

After all validation done and assigning old value and updating the dependecy property, i need to cal UpdateTarget() method, to Update the value in my TextBox.

这是对解决方案的更好解释

This is what explains the solution better

http://social.msdn.microsoft.com / forums / zh-CN / wpf / thread / c404360c-8e31-4a85-9762-0324ed8812ef /

这篇关于WPF:TextBox文本未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆