C# 数据绑定在WPF中不起作用
我无法将数据绑定到UI 问题C# 数据绑定在WPF中不起作用,c#,wpf,data-binding,C#,Wpf,Data Binding,我无法将数据绑定到UI 问题 我必须实现DependencyProperty还是INotifyPropertyChanged?它是简单的数据绑定 我想我不必使用上述属性 XMAL代码 <Window x:Class="Pr.child" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
我必须实现DependencyProperty还是INotifyPropertyChanged?它是简单的数据绑定 我想我不必使用上述属性 XMAL代码
<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>
子窗口类
public partial class child : Window
{
public child ()
{
InitializeComponent();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
RunTest runTest = new RunTest();
}
}
运行测试运行测试
class RunTest
{
public RunTest()
{
test t = new test();
t.info = "test info";
t.getinfo();
}
}
测试班
class test
{
public string info { get; set; }
public string getinfo()
{
return info;
}
}
调试信息
System.Windows.Data Warning: 52 : Created BindingExpression (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>
System.Windows.Data警告:52:为绑定(hash=6363067)创建了BindingExpression(hash=44780731)
System.Windows.Data警告:54:路径:“”
System.Windows.Data警告:56:BindingExpression(哈希=44780731):默认模式解析为单向
System.Windows.Data警告:57:BindingExpression(哈希=44780731):默认更新触发器已解析为PropertyChanged
System.Windows.Data警告:58:BindingExpression(哈希=44780731):附加到System.Windows.Controls.Label.Content(哈希=1867017)
System.Windows.Data警告:63:BindingExpression(哈希=44780731):正在解析源
System.Windows.Data警告:66:BindingExpression(哈希=44780731):找到数据上下文元素:(确定)
System.Windows.Data警告:73:BindingExpression(哈希=44780731):使用ObjectDataProvider(哈希=16906910)中的数据
System.Windows.Data警告:74:BindingExpression(哈希=44780731):使用根项激活
System.Windows.Data警告:100:BindingExpression(哈希=44780731):使用访问器{DependencyProperty.UnsetValue}将级别0处的项替换为
System.Windows.Data警告:97:BindingExpression(hash=44780731):使用以下命令获取级别0处的值:
System.Windows.Data警告:76:BindingExpression(哈希=44780731):TransferValue-获取原始值
System.Windows.Data警告:85:BindingExpression(哈希=44780731):TransferValue-使用最终值
我15分钟前遇到了这种情况。您需要实现DependencyProperty,这似乎是有意义的。ObjectDataProvider正在创建一个新的测试
,并对其调用getinfo
方法。这是空的,因为没有人有机会设置新实例的信息,事实上,您的代码甚至没有对它的引用
因此,标签正确地显示了null的值,您的跟踪与此一致
如果希望UI对绑定对象的其他更改作出反应,则只需实现INPC或使用DependencyObjects。但它有助于有一些属性绑定到 你的代码毫无意义。。。
RunTest
类只创建了一个从未在任何地方使用过的test
实例,为什么您希望ObjectDataProvider
使用这个特定的test
实例?它只会创建一个新实例。。。你想达到什么目标?
System.Windows.Data Warning: 52 : Created BindingExpression (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>