Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Custom usercontrol binding problem
6 replies. Latest Post by pepperit.be on January 12, 2009.
(0)
pepperit.be
Member
1 points
3 Posts
01-12-2009 6:37 AM |
Hello,
I'm a silverlight newbie, I 've created the following simple "reusable" textbox control. I want to use my control on several pages. I want to be able to bind from my page the Text property of the TextBox inside my control. I added a "Text" property on my control therefore.
MY REUSABLE CONTROL:
<UserControl x:Class="SilverlightApplication1.Controls.MyTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-namespace:SilverlightApplication1.Controls" > <Grid x:Name="LayoutRoot" Background="White"> <StackPanel Orientation="Horizontal"> <TextBox x:Name="txtTextBox"></TextBox> <TextBlock x:Name="label">This is a test</TextBlock> </StackPanel> </Grid></UserControl>
Code-Behind:
In the code behind of my control I have added the following property:
public string Text{ get { return txtTextBox.Text; } set { txtTextBox.Text = value; }}
MY PAGE LOOK LIKE THIS:
<UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:SilverlightApplication1.Controls" Width="400" Height="300" Loaded="UserControl_Loaded"> <Grid x:Name="LayoutRoot" Background="White"> <controls:MyTextBox Text="{Binding MyText}"></controls:MyTextBox> </Grid></UserControl>
Code-behind of the Page:
private void UserControl_Loaded(object sender, RoutedEventArgs e) { TestClass t = new TestClass(); t.MyText = "Helllooo!"; this.DataContext = t; }
When I run this code, Silverlight throws an exception in the InitializeComponent of the Page class, even before the Loaded method is called.
System.Windows.Markup.XamlParseExceptionAG_E_PARSER_BAD_PROPERTY_VALUE [Line: 8 Position: 35]
Line 8 is the line where is this line : "<controls:MyTextBox Text="{Binding MyText}"></controls:MyTextBox>"
When I change line 8 to this : "<controls:MyTextBox Text="hello"></controls:MyTextBox>"it works perfeclty ?
Any one an idea ?
davidezo...
Contributor
5682 points
871 Posts
01-12-2009 6:44 AM |
Hi,
what's the definition of your TestClass? Is "MyText" defined as a public property?
01-12-2009 7:00 AM |
Yes, it is.
public class TestClass : INotifyPropertyChanged { private string _myText; public string MyText { get { return _myText; } set { _myText = value; OnPropertyChanged("MyText"); } } protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; }
01-12-2009 7:28 AM |
The "Text" property of your user control must be defined as a dependency property if you want to set it in XAML.
murtaza....
Participant
1659 points
378 Posts
01-12-2009 7:40 AM |
The Issue with you is that your property of your control is simple property
but databinding can be applied to only dependency properties
you can implement your property in the userControl like
public
{
}
after you create your property like this you will able to get value from databinding
Please mark the answer if it solves your problem
01-12-2009 8:35 AM |
Read this article to correctly define a dependency property:
http://blogs.sqlxml.org/bryantlikes/archive/2008/12/15/silverlight-dependency-properties.aspx
01-12-2009 8:53 AM |
It works, thank you !