index > Windows Presentation Foundation ("Avalon") > Databinding dropped in composite control with DP

Databinding dropped in composite control with DP

Hi,

I have a databinding problem with a composite control I am creating.
The composite control consists of a Label and a TextBox, they both bind to dependency properties on the composite control, which then again binds to a property on an object defined in XmlDataProvider.
It seems that the databinding on the Text dependency property on the composite control is dropped/detached when I change the Text property on the TextBox is changed.

// UserControl1.xaml
<UserControl x:Class="CustomControls.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal">
<Label Name="label" />
<TextBox Name="textBox" />
</StackPanel>
</UserControl>

// UserControl1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace CustomControls
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>

public partial class UserControl1 : System.Windows.Controls.UserControl
{

public UserControl1()
{
InitializeComponent();

// --> Setup bindings
Binding labelContentBinding = new Binding("Label");
labelContentBinding.Source = this;
label.SetBinding(System.Windows.Controls.Label.ContentProperty, labelContentBinding);

Binding tbTextBinding = new Binding("Text");
tbTextBinding.Source = this;
textBox.SetBinding(TextBox.TextProperty, tbTextBinding);
// <-- Setup bindings
}

public static DependencyProperty LabelProperty =
DependencyProperty.Register("Label", typeof(string), typeof(UserControl1));

public static DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(UserControl1));

public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}

public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
}

And here is an example:
<Window x:Class="CustomControlApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customcontrols="clr-namespace:CustomControls;assembly=CustomControls"
Title="CustomControlApp" Height="300" Width="300"
>
<Window.Resources>
<XmlDataProvider x:Key="Family" XPath="/Family/Person">
<x:XData>
<Family xmlns="">
<Person Name="Tom" Age="9" />
<Person Name="John" Age="11" />
<Person Name="Melissa" Age="36" />
</Family>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<StackPanel Orientation="Vertical" DataContext="{StaticResource Family}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding XPath=@Name}" />
<TextBox Width="100" Name="textBox2" Text="{Binding XPath=@Name}" />
</StackPanel>
<customcontrols:UserControl1 Label="{Binding XPath=@Name}" Text="{Binding XPath=@Name}" />
</StackPanel>
</Window>

Hope someone can help me solve this.
Jesper Kleis Jensen
In your scenario, you should mark the binding on the Text DP as a TwoWay binding:
Zhou Yong
Thank you, I can't believe I missed that ;)
Jesper Kleis Jensen
reply 3

You can use google to search for other answers

 

More Articles

• How does WPF Handle simple control focus?
• Fontsize relative to the window size
• Replace Style specified in DataTemplate
• Installer that requieres WPF
• Cannot set Name attribute value
• HwndHost and Menu Items
• PageFunction Base Class
• LocBaml doesn't work with RC1?
• window allways on top
• STA error when creating scrollviewer in background thread?
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Rotating a grid
• Binding to the visual tree of a DataTemp
• Data Binding - SetBinding destroys Valid
• List View Control Template
• Yet Another Binding Question
• WPF Architectural Question
• Pass mouseevents to Inkcanvas childobjects
• Validating with Regular Expressions
• Where is OpenFileDialog?
• Managing Navigation Exceptions
• how to get selected area of text in an e
• fisheye effect transformation, is it pos
• Help search problem
• Issue with LostFocus UpdateSourceTrigger
• WPF application template....

Hot Articles

• Two-way binding with custom control
• using hatchbrush (system.drawing.drawing
• FlowDocumentPageViewer aspect ratio lock
• How is AutoComplete in TextBox mounted?
• Post file in WinFX
• How to bind a Canvas's TranslateTransfor
• Disabling Edits/Selections on FlowDocume
• Will WPF *EVER* work under W2K?
• How to auto-size ListView column headers
• How to generate XSD from custom XAML ser
• Dockable windows tool for WPF
• I think this is a design hole in WPF Dat
• Pack Uri throws exception
• SpeedRatio BUG?? - OK I'll Try this again
• Creating Custom- / User-Controls - best

Recommend Articles

• not getting an expected XBAP SecurityExc
• Setting data source from codebehind?
• How to transfer(Upload) file to remote s
• Click Event happening twice
• Syntax for Binding Attached Properties
• New XBAP launch screen
• Change the image dynamically from the St
• Cannot re-initialize ResourceDictionary
• UserControl: Use of Custom Control and E
• How to control line breaking in TextBloc
• How to get the positon of the control's
• Custom Popup Placement in control template
• Vertical ScrollBar Control Template
• GroupStyle and WrapPanel
• Using xaml