Вопрос

I know, maybe it is a simple question, bu I don't know how to realiza this:

I have my xaml code:

<phone:PhoneApplicationPage.Background>
        <ImageBrush ImageSource="/conTgo;component/resources/images/bg_settings.png" Stretch="None"/>
    </phone:PhoneApplicationPage.Background>
    <TextBlock TextWrapping="Wrap" Text="{Binding VersionNumber}" Foreground="{StaticResource PhoneAccentBrush}" FontFamily="Segoe WP Black" FontSize="26.667" TextAlignment="Center" LineHeight="16"/>
</phone:PhoneApplicationPage>

In my code behind I have:

 public string VersionNumber { get; set; }

How can I realise this?

Это было полезно?

Решение

The MVVM pattern is highly recommended for Silverlight development and works well for cases like this as well as setting your code up better for unit testing.

However, If your property that your binding to resides directly in your control and you want to keep it there then your control will need to implement INotifyPropertyChanged in order for the property to hook into Silverlight's (or WPF's) change notification:

public class YourControl : Control, INotifyPropertyChanged
{

    public string VersionNumber {
        get { return versionNumber; }
        set {
            versionNumber = value;
            NotifyPropertyChanged("VersionNumber");
        }         
    }
    private string versionNumber;

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

Again, I would definitely recommend a MVVM approach however.

Другие советы

The binding searches the property on your datacontext. So if you've added the property to your page, then you must set your page as it's own datacontext.

In your page's constructor, after the call to "InitializeComponent()", add:

this.DataContext = this;

That should do the trick.

This method is fine for small apps. If you want to make bigger and more structured app, you might want to learn about the MVVM pattern.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top