patryk
patryk

Reputation: 651

Binding to a custom property of a custom control property isn't working

I am working on a custom control. I want it to support data-binding, so it is as easy to maintain as the built-in controls are.

Here is what I got so far:

And the thing is that if not binding in my window xaml to the Matrix.Title property i.e. giving that value statically in xaml like Title="some title" the binding inside my control code works (I can even do customMatrix.Title += "1"; in my window and it works). However, when binding to the Matrix.Title property (like I did in the code provided), it does not work and I got a blank title in my view. Just to state it clearly, binding to default controls in the window works.

How to make the Matrix.Title property bindable?

Upvotes: 1

Views: 1417

Answers (1)

Mark Feldman
Mark Feldman

Reputation: 16148

The TextBlock binding on your Matrix control is wrong.

You've set up a dependency property and then the code in your Window.xaml is binding that property to Title in your Window class. That part is all fine, although you don't need the TitleChanged function (the binding gets done automatically so you don't need to do it manually yourself). The problem is that the TextBlock.Text property in your UserControl is declared like this:

<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}"/>

So it's trying to bind to the UserControl's current DataContext, and if you haven't set that (which you apparently haven't) then it inherits it from MainWindow which may or may not be what you want it to be. To fix this you need to bind the TextBlock to the UserControl's property, not the DataContext property, and you do that with FindAncester:

<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type custom:Matrix}}, Path=Title}" />

UPDATED: Another way to do this is to give the UserControl in your Matrix.xaml file a name and bind to it with ElementName:

<UserControl x:Class="YourProjectNameHere.Matrix"
         xmlns:custom="clr-namespace:Wpftest2"
         ... more namespaces here
         mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" 
         Name="thisControl">
<Grid>
    <TextBlock Name="theTextBlock" Grid.Row="0" Grid.Column="1" Text="{Binding ElementName=thisControl, Path=Title}"/>
</Grid>

Upvotes: 2

Related Questions