简体   繁体   中英

Databind text to a string property

I'm new to Silverlight and the concept of data-binding and I still fail on resolving my problem. I haven't manage to find a solution after few days of research.

Here is my problem :

I correctly bind a String property to the text of my TextBlock as you can see below :

MainPage.xaml

<Grid  Background="Blue"  DataContext="{StaticResource WP8Displayable}">
        <TextBlock x:Name="tbCanvasTitle" TextWrapping="Wrap" Text="{Binding titleDisplayable}" FontWeight="Bold" HorizontalAlignment="Center"/>          
</Grid>

WP8Displayable.cs

public class WP8Displayable : IDisplayable, INotifyPropertyChanged
{

    public String title { get; set; }


    #region INotifyPropertyChanged Members


    public string titleDisplayable
    {
        get
        {
            return title;
        }
        set
        {
            if (title != value)
            {
                title = value;
                NotifyPropertyChanged("titleDisplayable");
            }
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify the page that a data context property changed
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    public void setTitle(String s)
    {
        this.title = s;

        NotifyPropertyChanged("titleDisplayable");
    }
}

I have a thread in my MainPage.xaml.cs that can instantiate one or more instance of WP8Displayable class. When one of the instance call setTitle(String s) the text in my TextBlock does not update, it seems that my DataContext is not set up properly.

EDIT :

My thread is launched in the MainPage.xaml.cs in the MainPage_Loaded(object sender, RoutedEventArgs e) method and does something like this :

  var instanceWP8Displayable = new WP8Displayable();
  //tbCanvasTitle.DataContext = instanceWP8Displayable;   HERE IS WHAT I WOULD LIKE TO DO ON XAML
  instanceWP8Displayable.setTitle("my Title");

EDIT 2 : App.xaml

<Application
x:Class="AMS.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:windows="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:ioC="clr-namespace:AMS.Controller">

<!--Application Resources-->
<Application.Resources>

    <windows:ResourceDictionary>
        <local:LocalizedStrings xmlns:local="clr-namespace:AMS" x:Key="LocalizedStrings"/>
        <ioC:Locator x:Key="Locator" x:Name="Locator" />
    </windows:ResourceDictionary>
</Application.Resources>

MainPage.xaml

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid  Background="Blue"  DataContext="{Binding Source={StaticResource Locator}, Path=WP8Displayable}">
        <TextBlock x:Name="tbCanvasTitle" TextWrapping="Wrap" Text="{Binding titleDisplayable}" FontWeight="Bold" HorizontalAlignment="Center"/>          
    </Grid>
    <Grid.DataContext>
        <local:WP8Displayable />
    </Grid.DataContext>
    </Grid>

How can I dynamically set up the DataContext in that case ? And Is it possible to link more than one instance to the same object ?

If anyone as a clue or feels that my question is not clear enough don't hesitate to tell me.

Thank you.

I would recommend you check out MVVMLight which will help you eliminate a lot of boiler plate code you have to write eg INotifyPropertyChanged . Further it provides you with an IoC Container (Inversion of Control) that is most commonly used for the task you are trying to perform. You can implement a simple version of one on your own (see bellow).

You can set the DataContext in XAML but you will need a class that provides the Object, so for instance you could write a class like this (I'm assuming you add it directly to the project and not in a subfolder):

public class Locator
{
    public WP8Displayable WP8Displayable
    {
        get { return new WP8Displayable(); }
    }
}

Next you will have to register the Locator class in the App.xaml so you can reference it from within your view:

<Application
    ...
    xmlns:windows="clr-namespace:System.Windows;assembly=System.Windows"
    xmlns:ioC="clr-namespace:YOURAPPNAME">

 <!--Application Resources-->

 <Application.Resources>      
     <windows:ResourceDictionary>
         <local:LocalizedStrings xmlns:local="clr-namespace:AMS" x:Key="LocalizedStrings"/>
         <ioC:Locator x:Key="Locator" x:Name="Locator" />
     </windows:ResourceDictionary>
 </Application.Resources>



        ...

</Application>

Now we can set the DataContext in XAML:

<Grid  Background="Blue"  DataContext="{Binding Source={StaticResource Locator}, Path=WP8Displayable}">
        <TextBlock x:Name="tbCanvasTitle" TextWrapping="Wrap" Text="{Binding titleDisplayable}" FontWeight="Bold" HorizontalAlignment="Center"/>          
</Grid>

HTH

Your way for declaration property is wrong. Plz Declare property as below;

#region INotifyPropertyChanged Members

public string _titleDisplayable;
public string titleDisplayable
{
    get
    {
        return _titleDisplayable;
    }
    set
    {
        if (_titleDisplayable != value)
        {
            _titleDisplayable = value;
            NotifyPropertyChanged("titleDisplayable");
        }
    }
}

and In class methods plz write below code:

public void setTitle(string s)
{
    this.titleDisplayable = s;

}

Plz changes ur code as per above code.

Thanks , Hitesh.

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