Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top