Reputation: 92
This seems so easy. I've been doing some much more completed data binding, and now I can't get this simple test to work. What am I doing wrong?
Disclaimer: It is late on Friday after a long week.
Here is my XAML file:
<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>
And the code-behind:
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();
}
}
}
And finally the debug output that shows it is working fine. But it doesn't!
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'
Upvotes: 1
Views: 658
Reputation: 19
You should set the datacontext into 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;
};
}
}
}
Upvotes: 0
Reputation: 77364
You bound your string property to the DataContext
of your TextBlock
. It's working as it should. I can only guess, but did you mean to bind to the Text
property of TextBlock
so you can see the text appear?
<TextBlock Text="{Binding ElementName=MainWindowControl, Path=Test" />
Upvotes: 3