Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Data Binding of user control property

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-23-2006   #1 (permalink)
luba
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 SpecsSystem Spec
Old 10-27-2006   #2 (permalink)
Robin Davies
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com 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 2005-2008

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 47 48 49 50 51