7

I have a very simple UserControl called CustomTextBox with this XAML:

<UserControl x:Class="CustomTextBox" ... >
    <Grid>
        <TextBox x:Name="InnerTextBox"/>
    </Grid>
</UserControl>

Now when I use CustomTextBox and want to do binding to InnerTextBox.Text, it does not work:

... {Binding ElementName=CustomTextBox, Path=InnerTextBox.Text}

I tried it another way, does not work as well:

... {Binding ElementName=CustomTextBox.InnerTextBox, Path=Text}

I know I can define a new dependency property called CustomTextBox.Text and then bind it to InnerTextBox.Text but I am planning to have custom controls with many properties and it is hell of a work to copy all of them just to support binding. Furthermore, copying/wrapping properties means each value is stored twice.

In WinForms, this was a matter simple inheritance and all the properties were available automatically. In WPF, inheritance of XAML controls is not possible and the properties are inaccessible.

Is there any simple way on how to set up binding from some control to UserControl's child element property?

Libor
  • 3,285
  • 1
  • 33
  • 41
  • 1
    you should use a new dependency property anyway. – ZSH Dec 01 '13 at 12:01
  • Thanks. Is there an official way to do this? I've read a book about WPF 4 and not found anything about this. I have not found a guidelines on "copying properties" or "exposing dependency properties" on MSDN either. – Libor Dec 01 '13 at 12:51
  • 1.in the code behind of your control create a new DP ,say we call it Text 2. in the xaml from your code : 3.make sure that the DataContext of the textBox is the UserControl – ZSH Dec 01 '13 at 13:50
  • copy it to note pad or something i cant post msg here, don't know why – ZSH Dec 01 '13 at 13:52
  • I see, thanks. Only the binding should be {Binding Text2} I suppose. – Libor Dec 01 '13 at 13:58
  • the '2' was for the next step 1.2.3... but sure as you see fit – ZSH Dec 01 '13 at 14:08
  • @ZSH You can write your comment as answer and I would accept it. – Libor Dec 01 '13 at 15:12

3 Answers3

3

1.in the code behind of your control create a new DP ,say we call it Text
2. in the xaml from your code :

<TextBox x:Name="InnerTextBox" Text={Binding Text}/> 


3.make sure that the DataContext of the textBox is the UserControl

ZSH
  • 905
  • 5
  • 15
0

Look at the Debug text inside the Output window of visual studio, it will give explanatory text for data binding errors.

FreddyFlares
  • 473
  • 6
  • 17
  • In my project, it says: BindingExpression path error: 'Text' property not found on 'object' ''CustomTextBox' (Name='CustomTextBox')'. BindingExpression:Path=Text; DataItem='CustomTextBox' (Name='CustomTextBox'); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') – Libor Dec 01 '13 at 14:00
  • It simply cannot traverse to "CustomTextBox.InnerTextBox.Text" - it always takes "CustomTextBox.Text" instead. – Libor Dec 01 '13 at 14:01
0

you can do a Inheritance. Your not creating the custom control properly. In Wpf Coustom Control there two parts

  1. C# part->Behavior of the control. In C# part you can do the Inheritance
  2. Xaml Part->UI or appearance of the control

refer the below links for more deatils

http://msdn.microsoft.com/en-us/library/cc295235.aspx

http://www.codeproject.com/Articles/17830/Creating-and-consuming-a-custom-WPF-control

http://www.codeproject.com/Articles/49802/Create-a-WPF-Custom-Control-Part-2

http://wpftutorial.net/HowToCreateACustomControl.html

Kumareshan
  • 1,311
  • 8
  • 8
  • Controls with XAML cannot be inherited - just controls with code-behind only, see http://stackoverflow.com/questions/269106/inheriting-from-a-usercontrol-in-wpf – Libor Dec 01 '13 at 14:24
  • In my project, both the base and derived controls are custom controls with both XAML and code-behind... – Libor Dec 01 '13 at 14:25