![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Data Binding of user control property Hello, I have built a user control that consists of label and textBox. I have added a dependancy property Text that is influates the text of the textBox. <UserControl x:Class="UserControl.InputField" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Name="grid1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Margin="2,11,1,17" Name="label1">Label</Label> <TextBox Name="textBox1" Margin="6,13.5,13.5,13.5" Grid.Column="1" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left"></TextBox> </Grid> </UserControl> using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace UserControl { /// <summary> /// Interaction logic for InputField.xaml /// </summary> public partial class InputField : System.Windows.Controls.UserControl { public InputField() { InitializeComponent(); SetupTextBindings(); UpdateTextBoxText(); } private void SetupTextBindings() { Binding binding = new Binding("Text"); binding.Source = this; //binding.Source = this.textBox1.Text; binding.Mode = BindingMode.TwoWay; this.SetBinding(TextProperty, binding); } /////////////////////// Text Box Text ///////////////////////////////////////// public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(InputField), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnTextChanged))); public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } private static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { InputField control = (InputField)obj; control.UpdateTextBoxText(); } private void UpdateTextBoxText() { textBox1.Text = Text; } } } In another window I put my control and try to bind the text property to some field of an class instance. <Window x:Class="UserControl.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="UserControl" Height="300" Width="300" xmlns:uc="clr-namespace:UserControl" > <StackPanel Name="grid1"> <uc:InputField Text="{Binding Path=Name ,Mode=TwoWay}"/> </StackPanel> </Window> The problem that I have is that the binding is one way. If i change the field :Name the text of the textBox chabges, but if I enter a new text in the TextBox it doesn't change the data in the field. (I have a class Person that have one property:Name, I binded grid1 to an instance of person) Can anyine please help me. Thank you. |
My System Specs![]() |
| | #2 (permalink) |
| Guest | RE: Data Binding of user control property public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(InputField), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnTextChanged))); "luba" wrote: > Hello, > > I have built a user control that consists of label and textBox. > I have added a dependancy property Text that is influates the text of > the textBox. > > <UserControl x:Class="UserControl.InputField" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> > <Grid Name="grid1"> > <Grid.ColumnDefinitions> > <ColumnDefinition Width="Auto"/> > <ColumnDefinition Width="*"/> > </Grid.ColumnDefinitions> > > <Label Margin="2,11,1,17" Name="label1">Label</Label> > <TextBox Name="textBox1" Margin="6,13.5,13.5,13.5" Grid.Column="1" > Height="20" VerticalAlignment="Top" > HorizontalAlignment="Left"></TextBox> > </Grid> > </UserControl> > > using System; > using System.Collections.Generic; > using System.Text; > using System.Windows; > using System.Windows.Controls; > using System.Windows.Data; > using System.Windows.Documents; > using System.Windows.Input; > using System.Windows.Media; > using System.Windows.Media.Imaging; > using System.Windows.Navigation; > using System.Windows.Shapes; > > namespace UserControl > { > /// <summary> > /// Interaction logic for InputField.xaml > /// </summary> > > public partial class InputField : > System.Windows.Controls.UserControl > { > public InputField() > { > InitializeComponent(); > > SetupTextBindings(); > > UpdateTextBoxText(); > } > > private void SetupTextBindings() > { > Binding binding = new Binding("Text"); > binding.Source = this; > //binding.Source = this.textBox1.Text; > binding.Mode = BindingMode.TwoWay; > > this.SetBinding(TextProperty, binding); > } > > /////////////////////// Text Box Text > ///////////////////////////////////////// > public static readonly DependencyProperty TextProperty = > DependencyProperty.Register( > "Text", typeof(string), typeof(InputField), > new FrameworkPropertyMetadata("", new > PropertyChangedCallback(OnTextChanged))); > > public string Text > { > get { return (string)GetValue(TextProperty); } > set { SetValue(TextProperty, value); } > } > > private static void OnTextChanged(DependencyObject obj, > DependencyPropertyChangedEventArgs args) > { > InputField control = (InputField)obj; > control.UpdateTextBoxText(); > } > > private void UpdateTextBoxText() > { > textBox1.Text = Text; > } > } > } > > In another window I put my control and try to bind the text property to > some field of an class instance. > > <Window x:Class="UserControl.Window1" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > Title="UserControl" Height="300" Width="300" > xmlns:uc="clr-namespace:UserControl" > > > <StackPanel Name="grid1"> > <uc:InputField Text="{Binding Path=Name ,Mode=TwoWay}"/> > </StackPanel> > </Window> > > The problem that I have is that the binding is one way. > If i change the field :Name the text of the textBox chabges, but if I > enter a new text in the TextBox it doesn't change the data in the > field. > > (I have a class Person that have one property:Name, I binded grid1 to > an instance of person) > > Can anyine please help me. > > Thank you. > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binding to properties of a user control | Dave | Avalon | 1 | 04-10-2008 08:29 AM |
| Binding to a custom dependency property | diffeq | Avalon | 1 | 07-02-2007 01:42 PM |
| Binding a ComboBox to in internal property | Keith Patrick | Avalon | 4 | 10-04-2006 08:40 AM |
| Data Binding in user control | Griff | Avalon | 3 | 01-10-2006 03:53 PM |
| Re: Data binding doesn't work for Property Tag syntax ? | Mikhail | Avalon | 0 | 01-10-2006 03:51 PM |