MichaelS
MichaelS

Reputation: 27

binding windows 8 textBlock in MVVM light

I 'm having problem with TextBlock/TextBox binding. The TextBlock doesn't display the property's content. When I 'm debugging my app, property has content. How you can do it? Xaml.

<TextBlock HorizontalAlignment="Left" Margin="730,191,0,0" TextWrapping="Wrap" Text="{Binding XmlContentFile, Mode=TwoWay}"   VerticalAlignment="Top" Height="429" Width="465"/>

I was finding simple code in web, but I didn't find code.

Code property

 public string XmlContentFile
        {
            get
            {
                return this.xmlContentFile;
            }
            set
            {
                this.xmlContentFile = value;

            }
        }

My DataContext

DataContext="{Binding Main, Source={StaticResource Locator}}">

Method load XML file to string variable

public async void  XmlContentLoad()
        {

            if (selectFile != null)
            {
                try
                {

                    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
                    StorageFile storageFile = await storageFolder.GetFileAsync(selectFile);
                    xmlFileTextContent = await FileIO.ReadTextAsync(storageFile);



                }
                catch (Exception)
                {
                    throw new Exception("Bug");
                }
            }
        }

Upvotes: 0

Views: 708

Answers (2)

Jared Bienz - MSFT
Jared Bienz - MSFT

Reputation: 3550

The problem is that your XmlContentFile property doesn't raise any notifications when it's changed. Your ViewModel needs to implement INotifyPropertyChanged and raise an event whenever any property has changed.

It's likely that your view and its data bindings are getting setup and executed before XmlContentLoad completes (it's asynchronous). If the binding has already completed before the data is loaded, the only way the binding will happen again is if the property raises a notification that it has changed.

It's also worth pointing out that in your XmlContentLoad method you're setting the private variable and not the public property.

xmlFileTextContent = await FileIO.ReadTextAsync(storageFile);

Setting the private variable will never raise property change notification even if you have the setter code wired up to raise the notification. You'll either need to change XmlContentLoad to set the property and have the OnPropertyChanged notification in the setter (recommended) or you'll need to call OnPropertyChanged after you set the private variable (not recommended).

Hope that helps.

Dev support, design support and more awesome goodness on the way: http://bit.ly/winappsupport

Upvotes: 1

AymenDaoudi
AymenDaoudi

Reputation: 8301

Make sure you are setting the Binding Source correctly :

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1" >
<Grid>
    <TextBlock Text="{Binding XmlContentFile, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FFF3A3A3"/>
</Grid>

and make sure as well you are setting the value of your property in the right place :

public partial class MainWindow : Window,INotifyPropertyChanged
{
    private string _xmlContentFile;
    public string XmlContentFile
    {
        get
        {
            return _xmlContentFile ;
        }
        set 
        { 
            _xmlContentFile = value;
            OnPropertyChanged("XmlContentFile");
        }
    }
    public MainWindow()
    {
        InitializeComponent();
    }


    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        XmlContentFile = "New Value !";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

True that this answer is not in MVVM, but that won't need much changings except that you will be needing to set your DataContext to your ViewModel.

Upvotes: 0

Related Questions