开发者

Databinding is not working in WPF

开发者 https://www.devze.com 2023-01-24 20:30 出处:网络
I am unable to get databinding to UI. Questions Do I have to implement DependencyProperty OR INotifyPropertyChanged ? Its simple databinding

I am unable to get databinding to UI.

Questions

Do I have to implement DependencyProperty OR INotifyPropertyChanged ? Its simple databinding and I am thinking I dont have to use above properties.

XMAL CODE

<Window x:Class="Pr.child"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:src="clr-namespace:Pr" 
     xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
     Title="" Height="327" Width="683" WindowStartupLocation="CenterOwner">
     <Window.Resources>
        <ObjectDataProvider x:Key="Obj" ObjectType="{x:Type  src:test}" MethodName="getinfo" />
      </Window.Resources>
      <Label  x:Name="lblTest" Content="{Binding Source={StaticResource Obj},diag:PresentationTraceSources.TraceLevel=High}"</Label>
      <Button  Name="btnOK" Click="btnOK_Click">OK</Button>

code Behind

Main Windows class

public partial class Window1 : Window
{
   child w = new child();
}

Child Window Class

public partial class child : Window
{
     public child ()
     {
         InitializeComponent();
     }
     private void btnOK_Click(object sender, RoutedEventArgs e)
     {
         RunTest runTest = new RunTest();
     }
 }

RunTest runTest

 class RunTest
 { 
     public RunTest()
     {
         test t = new test();
         t.info = "test info";
         t.getinfo();
     }
 }

Test Class

class test
{
     public string info { get; set; }
     public string getinfo()
     {
        return info;
     }
}

Debug Info

System.Windows.Data Warning: 52 : Created B开发者_运维技巧indingExpression (hash=44780731) for Binding (hash=63630067)
System.Windows.Data Warning: 54 :   Path: ''
System.Windows.Data Warning: 56 : BindingExpression (hash=44780731): Default mode resolved to OneWay
System.Windows.Data Warning: 57 : BindingExpression (hash=44780731): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 58 : BindingExpression (hash=44780731): Attach to System.Windows.Controls.Label.Content (hash=1867017)
System.Windows.Data Warning: 63 : BindingExpression (hash=44780731): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=44780731): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 : BindingExpression (hash=44780731): Use Data from ObjectDataProvider (hash=16906910)
System.Windows.Data Warning: 74 : BindingExpression (hash=44780731): Activate with root item <null>
System.Windows.Data Warning: 100 : BindingExpression (hash=44780731): Replace item at level 0 with <null>, using accessor {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 97 : BindingExpression (hash=44780731): GetValue at level 0 from <null> using <null>: <null>
System.Windows.Data Warning: 76 : BindingExpression (hash=44780731): TransferValue - got raw value <null>
System.Windows.Data Warning: 85 : BindingExpression (hash=44780731): TransferValue - using final value <null>


This seems to make sense. Your ObjectDataProvider is creating a new test, and calling the getinfo method on it. This is null, because nothing has had a chance to set the info on that new instance, in fact your code doesn't even have a reference to it.

So the label is correctly displaying the value of null, which your trace agrees with.

You only need to implement INPC or use DependencyObjects if you want your UI to react to changes that something else makes to your bound objects. But it helps to have some properties to bind to!


I just 15 minutes ago ran into this exact situation. You need to implement a DependencyProperty

0

精彩评论

暂无评论...
验证码 换一张
取 消