How to bind to a WPF dependency property when the datacontext of the page is used for other bindings?

How to bind to a WPF dependency property when the datacontext of the page is used for other bindings? (Simple question)

Bind to a parent/sibling of current datacontext/source property in WPF

How do we bind to a parent/sibling of a current datacontext (i.e. the source property representing the current datacontext)? I am not talking about binding to the parent control’s property (that case

Dependency Property WPF Grid

I want to Bind the textblock text in WPF datagrid to a dependency property. Somehow, nothing gets displayed, but when I use the same textblock binding outside the grid, everything works fine. Below is



Register Dependency Property Bind in Codebehind

The question is how to setup a dependency Property to bind it to any other Dependency Property and release it later. There is one Dependency Property in UserControl1 #Region INotifyPropertyChanged Me

How to bind to a WPF dependency property to window?

i create a dependency property to close a view from view model, dependencyProperty: public static class WindowBehaviors { public static readonly DependencyProperty IsOpenProperty = DependencyProperty

wpf how to bind to DataContext existance?

I set the datacontext dynamically in code. I would like a button on screen to be enabled/disabled depending if DataContext == null or not. I can do it in code when I assign the DataContext but it woul

Dependency Property In WPF/SilverLight

I have searched on google about how to get started with the dependency property used in WPF/silverlight but didn’t get any idea of the dependency property, can any one tell me about it , from beginner

Bind to a Dependency Property that is in parent’s DataContext

I would like to bind third column to a CollectionBindingTwo property that is within Window’s DataContext and not inside DataContext of an Items from CollectionBindingOne. By defining the second collec

WPF Dependency Property not being set

I am trying to bind a dependency property via XAML to my custom WPF control. Here’s how I register the dependency property: public static readonly DependencyProperty AltNamesProperty = DependencyPrope

Bind to wpf custom control dependency property for tooltip?

I have a custom control I wrote in WPF, which has a Boolean dependency property: public static readonly DependencyProperty IsAlertProperty = DependencyProperty.Register(IsAlert, typeof(bool), typeof

WPF – How to bind to a Dependency Property of custom class

I’m once again in WPF binding hell 🙂 I have a public class (Treatment) as follows: public class Treatment() { … public Ticker SoakTimeActual; … } Within Ticker is a Dependency Property: public c

Answers

In XAML:

Something="{Binding SomethingElse, ElementName=SomeElement}"

In code:

BindingOperations.SetBinding(obj, SomeClass.SomethingProperty, new Binding {
  Path = new PropertyPath(SomeElementType.SomethingElseProperty),  /* the UI property */
  Source = SomeElement /* the UI object */
});

Though usually you will do this the other way round and bind the UI property to the custom dependency property.

The datacontext of the element needed to be set.

XAML:

<Window x:Class="WpfDependencyPropertyTest.Window1" x:Name="mywindow">
   <StackPanel>
      <Label Content="{Binding Path=Test, ElementName=mywindow}" />
   </StackPanel>
</Window>

C#:

public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register("Test",
                                    typeof(string),
                                    typeof(Window1),
                                    new FrameworkPropertyMetadata("Test"));
public string Test
{
   get { return (string)this.GetValue(Window1.TestProperty); }
   set { this.SetValue(Window1.TestProperty, value); }
}

Also see this related question:

WPF DependencyProperties