You can get the existing Binding
using FrameworkElement.GetBindingExpression. Here's a sample:
FrameworkElement yourControl = null; // the code to get the control goes here
BindingExpression bindingExpression =
yourControl.GetBindingExpression(TextBox.Background);
Binding binding = bindingExpression.ParentBinding; // it's your binding
Then, create a new Binding
object, setup its properties (change target if required) and use SetBinding
to attach it.
UPDATE
Now about the binding source.
Note that Source
property will be non-null only if you set this property explicitly.
Other options for the Binding
source include:
- specific element: used if
Binding.ElementName
property is set
- templated control: used if
Binding.RelativeSource
is set to a RelativeSource
with Mode=RelativeSourceMode.TemplatedParent
- binding target control: used if
Binding.RelativeSource
is set to a RelativeSource
with Mode=RelativeSourceMode.TemplatedParent
DataContext
: if neither Source
, nor the previous 3 options apply.
If you want to clone a Binding
, you should check all these options.
However, if you just need the data item, you'd better use BindingExpression.DataItem
property, which should return the actual data item that is the binding source.