Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Data Binding of user control property

 
 
Old 10-23-2006   #1 (permalink)
luba


 
 

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 SpecsSystem Spec
Old 10-27-2006   #2 (permalink)
Robin Davies


 
 

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 SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46