Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Databinding to a dependancy property

Databinding to a dependancy property

Scheduled Pinned Locked Moved WCF and WF
wpfwcfcomhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaulPrice
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • P PaulPrice

      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

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • A ABitSmart

        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

        P Offline
        P Offline
        PaulPrice
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • P PaulPrice

          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

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          Glad I was of help :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups