Skip to main content

Microsoft Silverlight

Answered Question Custom usercontrol binding problemRSS Feed

(0)

pepperit.be
pepperit.be

Member

Member

1 points

3 Posts

Custom usercontrol binding problem

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.XamlParseException
AG_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 ? 
 

davidezordan
davidezo...

Contributor

Contributor

5682 points

871 Posts

Silverlight MVP

Re: Custom usercontrol binding problem

Hi,

what's the definition of your TestClass? Is "MyText" defined as a public property?

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

pepperit.be
pepperit.be

Member

Member

1 points

3 Posts

Re: Custom usercontrol binding problem

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;
    }
  

davidezordan
davidezo...

Contributor

Contributor

5682 points

871 Posts

Silverlight MVP

Re: Re: Custom usercontrol binding problem

The "Text" property of your user control must be defined as a dependency property if you want to set it in XAML.

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

murtaza.dharwala
murtaza....

Participant

Participant

1659 points

378 Posts

Re: Custom usercontrol binding problem

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 static readonly DependencyProperty TextProperty =  DependencyProperty.Register("Text",typeof(string),typeof(this),null);

public PointCollection Text

{

set { SetValue(TextProperty , value); }get { return (string)GetValue(TextProperty ); }

}

after you create your property like this you will able to get value from databinding

Please mark the answer if it solves your problem

Murtaza Dharwala
Diaspark Inc.
www.diaspark.com
email:murtaza.dharwala@diaspark.com


Please remember to click “Mark as Answer” on the post that helps you

davidezordan
davidezo...

Contributor

Contributor

5682 points

871 Posts

Silverlight MVP
Answered Question

Re: Re: Custom usercontrol binding problem

Read this article to correctly define a dependency property:

http://blogs.sqlxml.org/bryantlikes/archive/2008/12/15/silverlight-dependency-properties.aspx

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

pepperit.be
pepperit.be

Member

Member

1 points

3 Posts

Re: Re: Custom usercontrol binding problem

 It works, thank you !

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities