Databinding to a dependancy property
-
Hi all, I get the feeling i have missed the point somewhat here, but I am playing with (attempting to learn) dependancy properties and databinding. I have a very simple example application code behind
public partial class MainAppWindow : Window { public MainAppWindow() { InitializeComponent(); } public string Test { get { return (string)GetValue(TestProperty); } set { SetValue(TestProperty, value); } } public static readonly DependencyProperty TestProperty = DependencyProperty.Register("Test", typeof(string), typeof(MainAppWindow), new UIPropertyMetadata("Test String")); }
xaml
<Window x:Class="UserClient.MainAppWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BackUp" Height="500" Width="900"><TextBlock Text="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Test}"/>
</Window>
The problem I have is that I am unable to get the TextBlock to show any text, it does not appear to be binding to the dependancy property. Am I just being daft and missing the point, or am I doing something fundamentally wrong (well, i know I am doing something wrong, I just do not know what it is. Thanks for any advice. Paul.
Just racking up the postings
-
Hi all, I get the feeling i have missed the point somewhat here, but I am playing with (attempting to learn) dependancy properties and databinding. I have a very simple example application code behind
public partial class MainAppWindow : Window { public MainAppWindow() { InitializeComponent(); } public string Test { get { return (string)GetValue(TestProperty); } set { SetValue(TestProperty, value); } } public static readonly DependencyProperty TestProperty = DependencyProperty.Register("Test", typeof(string), typeof(MainAppWindow), new UIPropertyMetadata("Test String")); }
xaml
<Window x:Class="UserClient.MainAppWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BackUp" Height="500" Width="900"><TextBlock Text="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Test}"/>
</Window>
The problem I have is that I am unable to get the TextBlock to show any text, it does not appear to be binding to the dependancy property. Am I just being daft and missing the point, or am I doing something fundamentally wrong (well, i know I am doing something wrong, I just do not know what it is. Thanks for any advice. Paul.
Just racking up the postings
pprice wrote:
RelativeSource.Self
You are basically pointing to the itself(TextBlock), since you are saying Self.
pprice wrote:
Path=Test
While you are pointing to the TextBlock itself, you are asking for the property named "Test" in it. Which is not present and hence the binding is failing. What you actually should do is, point to the parent window which has your property. Something like this,
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Test}"
And you should see "Test String" in your TextBlock
-
pprice wrote:
RelativeSource.Self
You are basically pointing to the itself(TextBlock), since you are saying Self.
pprice wrote:
Path=Test
While you are pointing to the TextBlock itself, you are asking for the property named "Test" in it. Which is not present and hence the binding is failing. What you actually should do is, point to the parent window which has your property. Something like this,
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Test}"
And you should see "Test String" in your TextBlock
-
I had just sussed that out and was working out what I had to do to fix it. You just made that easy, thank you very much. Time and effor appreciated.
Just racking up the postings