![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | 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) |
| | 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 | |
| |