这似乎很容易。我一直在做一些更完整的数据绑定,现在我无法让这个简单的测试工作。我究竟做错了什么?
免责声明:经过漫长的一周后,周五晚了。
这是我的XAML文件:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="MainWindowControl"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase">
<Grid>
<TextBlock DataContext="{Binding ElementName=MainWindowControl, Path=Test, diag:PresentationTraceSources.TraceLevel=High}" />
</Grid>
</Window>
代码隐藏:
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string _test = "testing 1..2..3";
public string Test { get { return _test; } set { _test = value; } }
public MainWindow()
{
InitializeComponent();
}
}
}
最后显示它的调试输出工作正常。但事实并非如此!
System.Windows.Data Warning: 56 : Created BindingExpression (hash=24898218) for Binding (hash=15584387)
System.Windows.Data Warning: 58 : Path: 'Test'
System.Windows.Data Warning: 60 : BindingExpression (hash=24898218): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=24898218): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=24898218): Attach to System.Windows.Controls.TextBlock.DataContext (hash=61116530)
System.Windows.Data Warning: 67 : BindingExpression (hash=24898218): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=24898218): Found data context element: <null> (OK)
System.Windows.Data Warning: 74 : Lookup name MainWindowControl: queried TextBlock (hash=61116530)
System.Windows.Data Warning: 78 : BindingExpression (hash=24898218): Activate with root item MainWindow (hash=22749765)
System.Windows.Data Warning: 108 : BindingExpression (hash=24898218): At level 0 - for MainWindow.Test found accessor RuntimePropertyInfo(Test)
System.Windows.Data Warning: 104 : BindingExpression (hash=24898218): Replace item at level 0 with MainWindow (hash=22749765), using accessor RuntimePropertyInfo(Test)
System.Windows.Data Warning: 101 : BindingExpression (hash=24898218): GetValue at level 0 from MainWindow (hash=22749765) using RuntimePropertyInfo(Test): 'testing 1..2..3'
System.Windows.Data Warning: 80 : BindingExpression (hash=24898218): TransferValue - got raw value 'testing 1..2..3'
System.Windows.Data Warning: 89 : BindingExpression (hash=24898218): TransferValue - using final value 'testing 1..2..3'
答案 0 :(得分:3)
您将字符串属性绑定到DataContext
的{{1}}。它正常运作。我只能猜测,但你的意思是绑定到TextBlock
的{{1}}属性,这样你才能看到文字出现?
Text
答案 1 :(得分:0)
您应该将datacontext设置为codebehind。
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string _test = "testing 1..2..3";
public string Test { get { return _test; } set { _test = value; } }
public MainWindow()
{
InitializeComponent();
this.Loaded+=(s,a)=>
{
this.DataContext=Test;
};
}
}
}