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. WPF
  4. Custom Binding Extension -- how to access source object.?

Custom Binding Extension -- how to access source object.?

Scheduled Pinned Locked Moved WPF
wpfcsharptutorialquestionwcf
2 Posts 1 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.
  • Q Offline
    Q Offline
    quetzilla
    wrote on last edited by
    #1

    Using this excellent article[^] I am trying to set up a custom binding extension. The issue is that when the binding finds the source object, I need to do some stuff to the source object, but I don't know where to put that code. For example, I have the following class:

    class MyObject {
    public string Value { get; set; }
    }

    and some XAML in MainWindow.xaml:

    <Button Content="{my:CustomBinding Value}">

    And set the data context in code:

    public MainWindow()
    {
    InitializeComponent();
    this.DataContext = MyObject;
    }

    That much works, the Value string gets displayed on the button. But what I need to do is write code in the CustomBinding class that operates on MyObject as soon as possible, which means I need to resolve MyObject from the BindingExpression. What I've figured out so far is that when the CustomBinding instance is created, it doesn't actually find the MyObject.Value because it needs to find the data context first in order to resolve the BindingExpression, so it doesn't actually find MyObject until the MainWindow gets laid out on screen. So my question is, what code do I write in the CustomBindingExtension class so that it runs when the WPF layout system decides to resolve the BindingExpression to find MyObject?

    Q 1 Reply Last reply
    0
    • Q quetzilla

      Using this excellent article[^] I am trying to set up a custom binding extension. The issue is that when the binding finds the source object, I need to do some stuff to the source object, but I don't know where to put that code. For example, I have the following class:

      class MyObject {
      public string Value { get; set; }
      }

      and some XAML in MainWindow.xaml:

      <Button Content="{my:CustomBinding Value}">

      And set the data context in code:

      public MainWindow()
      {
      InitializeComponent();
      this.DataContext = MyObject;
      }

      That much works, the Value string gets displayed on the button. But what I need to do is write code in the CustomBinding class that operates on MyObject as soon as possible, which means I need to resolve MyObject from the BindingExpression. What I've figured out so far is that when the CustomBinding instance is created, it doesn't actually find the MyObject.Value because it needs to find the data context first in order to resolve the BindingExpression, so it doesn't actually find MyObject until the MainWindow gets laid out on screen. So my question is, what code do I write in the CustomBindingExtension class so that it runs when the WPF layout system decides to resolve the BindingExpression to find MyObject?

      Q Offline
      Q Offline
      quetzilla
      wrote on last edited by
      #2

      For anyone interested, the answer turned out to be to hook into the DataContextChanged event of the targeted UI element (button1.DataContextChanged in this case).

      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