简体   繁体   中英

Confusion on Silverlight user control custom property binding

I have UserControl, named thisUserControl , it has two controls as shown in the following XAML code:

<UserControl
    ...
    x:Name="thisUserControl">
    <StackPanel x:Name="LayoutRoot">
        <Button x:Name="btnChangeString" Content="Change String" Click="btnChangeString_Click" />
        <TextBlock Text="{Binding Path=StringProperty, ElementName=thisUserControl}" />
    </StackPanel>
</UserControl>

BTW, I don't know why I have to set a name for UserControl so that I could use it for Binding in ElementName=thisUserControl . (I thought it should be this by default if ElementName is not provided)

I also defined the customer property for this UserControl, with some other code:

private string sp = "A String Here";
[Bindable(true)]
public string StringProperty
{
    get { return sp; }
    set { sp = value; }
}

private void btnChangeString_Click(object sender, RoutedEventArgs e)
{
    StringProperty = "Something Else";
}

When I compile and run it, click the "Change String" button, nothing happens.

It seems the Bindable(true) not working at all. What does this Bindable(true) mean?

Is there an easy way to make a Custom Property bindable? Let this UserControl inherit INotifyPropertyChanged ? or use DependencyProperty ?

Bindable(true) is a way for you to tell a GUI designer that a property supports two-way binding. However, to actually implement two-way binding, you have to implement INotifyPropertyChanged for that property.

Also, if ElementName is not provided in a binding, the default is to bind to DataContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM