locked
Binding data to TextBlock RRS feed

  • Question

  • Hello,

                  I have successfully binded data to listboxes and datagrids.  However, I am running into a very weird issue binding a simple piece of data to a textblock.  I have reproduced the issue with the following sample code:

    namespace test

    {

        public class Test: INotifyPropertyChanged

        {

            public event PropertyChangedEventHandler PropertyChanged;

            public Test(){a = "Testing";}

            private string A;

            public string a

            {

                get

                {

                    return A;

                }

                set

                {

                    A = value;

                    NotifyPropertyChanged("A");

                }

            }

            private void NotifyPropertyChanged(string propertyName)

            {

                if (PropertyChanged != null)

                    PropertyChanged(this, new

                    PropertyChangedEventArgs(propertyName));

            }

        }

     

        public partial class Page : UserControl

        {

           

            public Page()

            {

                InitializeComponent();

               

                Test mytest = new Test();

                queryInfo.DataContext = mytest;

                mytest.a = "The value I want to see....";

            }

        }

    }

    I am using expression blend 2 SP1.  It generated the following xaml:

    <UserControl x:Class="test.Page"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Width="400" Height="300" xmlns:test="clr-namespace:test" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

          <UserControl.Resources>

                <test:Test x:Key="TestDS" d:IsDataSource="True"/>

          </UserControl.Resources>

        <Grid x:Name="LayoutRoot" Background="White">

     

          <TextBlock HorizontalAlignment="Stretch" x:Name="queryInfo" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding Mode=OneWay, Path=a, Source={StaticResource TestDS}}"/>

     

        </Grid>

    </UserControl>

    When I run this sample program, the queryInfo textbox shows: “Testing”.  I would have expected to see: "The value I want to see....".

    I tried changing the binding in the xaml to:

    Text="{Binding a}"

    It did not make a difference.  I am not sure what I am doing wrong.  I am very new to Expression Blend and Silverlight.  I am hoping you may be able to help me.  Thank you in advance for your help…

    Sincerely,

    Didier

     

    Saturday, June 20, 2009 1:04 AM

Answers

  • Thank you for the reply.  I really appreciate your help.  You are correct, I would only use the property-change event for data that comes from a datasource that dynamically changes.  The above code was simply for a test because I could not bind any value to my textBlock.  

    It turns out that to fix my issue I had to set the datacontext of the container (ie: a datagrid) instead of the textblock itself.  Once I made this change, everything worked well.

    The other thing I found out was that I had to set the datacontext of the container programatically:

    LayoutRoot.DataContext = mytest;


    If I did it just in expression blend than it would not work.  I find that strange.  I am not sure if that is a bug or I am missing something.

    Thanks again for all your support,
    Didier           

    • Marked as answer by ddaoliver Monday, October 19, 2009 3:47 PM
    Monday, June 22, 2009 1:35 PM

All replies

  • Normally you use the PropertyChange-Event when you have a Source for binding on it (e.g. a Database) where the original text comes from.
    The Change-Event then fires up when you change the value.

    In you example you instanciate the Test-Class and set a = "Testing";
    So, everytime you'll change the Text from the Textbox it will be changed back to testing.

    The firsttime the event is fired up is in the Constructor for the Page. There you want to set the Text of the Textbox.

    Monday, June 22, 2009 8:51 AM
  • Thank you for the reply.  I really appreciate your help.  You are correct, I would only use the property-change event for data that comes from a datasource that dynamically changes.  The above code was simply for a test because I could not bind any value to my textBlock.  

    It turns out that to fix my issue I had to set the datacontext of the container (ie: a datagrid) instead of the textblock itself.  Once I made this change, everything worked well.

    The other thing I found out was that I had to set the datacontext of the container programatically:

    LayoutRoot.DataContext = mytest;


    If I did it just in expression blend than it would not work.  I find that strange.  I am not sure if that is a bug or I am missing something.

    Thanks again for all your support,
    Didier           

    • Marked as answer by ddaoliver Monday, October 19, 2009 3:47 PM
    Monday, June 22, 2009 1:35 PM