WPF数据绑定重新路由-将新值应用于资源不会影响绑定控件

WPF数据绑定重新路由-将新值应用于资源不会影响绑定控件,wpf,data-binding,wpf-controls,binding,Wpf,Data Binding,Wpf Controls,Binding,我的代码如下: Xaml: 这段代码工作正常,因为我的类实现了INotifyPropertyChanged,但当我想将属性应用于它自己的追索权时,它不会应用于绑定到该属性的文本框 HandHeld hh = this.FindResource("hh") as HandHeld; hh=new HandHeld(); //this line doest not affect. why ? 或者这也不管用 this.resources["hh"]=new HandHeld();//this do

我的代码如下:
Xaml:

这段代码工作正常,因为我的类实现了INotifyPropertyChanged,但当我想将属性应用于它自己的追索权时,它不会应用于绑定到该属性的文本框

HandHeld hh = this.FindResource("hh") as HandHeld;
hh=new HandHeld(); //this line doest not affect. why ?
或者这也不管用

this.resources["hh"]=new HandHeld();//this doesnt have any affect too.
为什么?首先

HandHeld hh = this.FindResource("hh") as HandHeld;
hh=new HandHeld(); 
&

它们不一样。第一个实现使用代码中的对象声明(这显然没有任何效果),而第二个实现使用类掌上型的xaml对象

第二个选项无效,因为在设置对象时,没有任何内容通知更改。因此,您要么实现一个,要么在当前代码中创建一个依赖属性

public static readonly DependencyProperty HandHeldObjProperty =
            DependencyProperty.Register("HandHeldObj", typeof(HandHeld), typeof(UserControl),new PropertyMetadata(null));            
将XAML中的上述依赖项属性绑定到源。当你想设定值时

SetValue(HandHeldObjProperty, new HandHeld());

编辑:

我认为你有个地方特别想把你的东西绑起来。类似于检查对象(手持对象)是否为空

如果不是这样,那么您编写的代码应该可以工作。启动类后是否设置了依赖属性HandHeldName?我是说

this.resources["hh"]=new HandHeld();
hh.HandHeldName="testing";

好的,你可以这样做

  • 将StaticResource绑定更改为DynamicResource(确保在hh被称为StaticResource的任何地方都这样做)


  • 请验证并让我知道这是否有效。

    好的,亲爱的Loxxy,我如何通知我的表单及其控件新值在参考资料[“hh”]中?在我的案例中,我应该如何使用HandHeldObj和DependencyProperty?或者如何将此属性绑定到Xaml控件?提前谢谢
    this.resources["hh"]=new HandHeld();
    
    public static readonly DependencyProperty HandHeldObjProperty =
                DependencyProperty.Register("HandHeldObj", typeof(HandHeld), typeof(UserControl),new PropertyMetadata(null));            
    
    SetValue(HandHeldObjProperty, new HandHeld());
    
    this.resources["hh"]=new HandHeld();
    hh.HandHeldName="testing";
    
    <TextBox  Name="txtHHName" Text="{Binding Source={DynamicResource hh}, Path=HandHeldName, Mode=TwoWay}" />
    
    this.Resources.Remove("hh");
    this.Resources.Add("hh", new HandHeld()); //this line should take affect.