|
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? |
|