Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up binding to a custom DependencyProperty inside a WPF user control

I have a WPF UserControl containing a custom DependencyProperty named MyDP. I want to bind this to a property on my ViewModel (which is injected as the UserControl's DataContext). I know one way to do it by setting the binding in the UserControl's declaration in the parent window's XAML as such:

<Window x:Class="MyNamespace.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:views="clr-namespace:MyNamespace.Views">
    <StackPanel>
        <views:MyControl MyDP="{Binding Path=MyVMProperty, Mode=OneWayToSource}"/>
    </StackPanel>
</Window>

This works fine, but as an alternate could I set up the binding inside the UserControl's XAML, similar to how I set the bindings for the individual controls inside the UserControl to other properties of the ViewModel?

like image 596
Tim Trout Avatar asked Feb 17 '10 22:02

Tim Trout


2 Answers

You can't do what you were originally thinking directly. You probably tried and got some compile errors. You can't set a custom property inline in the UserControl's root XAML because the element type is UserControl so the compiler is enforcing property names based on that type, not your custom type. You could get around this by changing to an Attached Property but that actually changes the meaning of MyDP. Instead you can set a default in the Style for the UserControl and get an additional benefit of being able to override it on any declared instance by just doing what's in your original example. Set this under your UserControl's root element:

<UserControl.Style>
    <Style>
        <Setter Property="views:MyControl.MyDp" Value="{Binding Path=MyVMProperty, Mode=OneWayToSource}" />
    </Style>
</UserControl.Style>
like image 86
John Bowen Avatar answered Nov 09 '22 08:11

John Bowen


You could also define the binding in the constructor of MainWindow, like this:

public MainWindow()
{
    InitializeComponent();
    SetBinding(MyDPProperty, "MyVMProperty");
}
like image 44
Horpe Avatar answered Nov 09 '22 08:11

Horpe