Expression > Forums Home > Related Technologies Forums > Windows Presentation Foundation (WPF) > How do I set the text of a TextBlock to a global variable?
Ask a questionAsk a question
 

AnswerHow do I set the text of a TextBlock to a global variable?

  • Friday, November 06, 2009 10:10 PMBruCru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have am writing a simple accounting program that has a datagrid with a textblock in the header.  I want to be able to bind the textblock's Text to a global variable (Company Name).  Anyone have any ideas?

    Thanks
    Chris

Answers

  • Tuesday, November 10, 2009 3:34 PMTimmyGR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You're just looking for a standard binding then.  The source of your binding could be set using the {x:Static} markup extension.  Something like:

    For instance,


    namespace MyNamespace
    {
        public class YourSingletonClass : INotifyPropertyChanged
        {
          private string _companyName;
          
          public string CompanyName
          {
            get { return _companyName; }
            set
            {
                _companyName = value;

                // raise property changed here

             } 
        }

        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public static YourSingletonClass  YourInstance;
            public Window1()
            {
                InitializeComponent();
            }
         }

    <Window x:Class="MyNamespace.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1"
            xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly">
        <Grid>
            <TextBlock Text="{Binding Source={x:Static local:Window1.YourInstance}, Path=CompanyName}" />
        </Grid>
    </Window>

    Of course, you could move the CompanyName property directly to Window1, and then you could just do:

    <Window x:Class="MyNamespace.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1"
            xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
            x:Name="root">
        <Grid>
            <TextBlock Text="{Binding ElementName=root, Path=CompanyName}" />
        </Grid>
    </Window>


All Replies

  • Friday, November 06, 2009 10:57 PMTimmyGR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Create a static string somewhere

    namespace MyNamespace
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public static string CompanyName = "My Company's Name";
            public Window1()
            {
                InitializeComponent();
            }
         }
    }
    

    Then, in XAML:
    <Window x:Class="MyNamespace.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1"
            xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly">
        <Grid>
            <TextBlock Text="{x:Static local:Window1.CompanyName}" />
        </Grid>
    </Window>
    

    Hope this helps.

     

  • Saturday, November 07, 2009 1:52 PMRammohan Ammiti Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    globally u can declare  like public static string CompanyName = "urname";


    xaml u can write


     xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"> call the application namespace


       <TextBlock Text="{x:Static local:Window1.urname}" />

  • Monday, November 09, 2009 7:21 PMBruCru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Thanks Tim.  However, when I try this I get the following error:

    "Error 1 Cannot find the static member 'sCompany' on the type 'Window1'."

    I have the following code in vb:

     

     

     

    Public Sub New()
    
    InitializeComponent()
    
    Static sCompanyName As String = sCompany.Trim
    
    
    



    I then try to call it in XAML with:

    Text="{x:Static local:Window1.sCompany}"
    
    I have declared a local namespace as:

    xmlns:local="clr-namespace:Balla_Accounting"
    
    where Balla_Accounting is the name of my project.

    Thanks for your help.
    Chris

  • Monday, November 09, 2009 8:15 PMTimmyGR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You need that static sComanyName to be declared as public.

    public static sCompanyName as string = "MyCompany"
  • Monday, November 09, 2009 8:51 PMBruCru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I just realized that this limits me textblock to a variable that can not be changed unless I retstart my application.  The problem is that I may change the company based on a combobox selection.  I would then want the textblock to refer to the new company.  Do you know of a way to bind the textblock to a non-Static variable?
  • Tuesday, November 10, 2009 3:34 PMTimmyGR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You're just looking for a standard binding then.  The source of your binding could be set using the {x:Static} markup extension.  Something like:

    For instance,


    namespace MyNamespace
    {
        public class YourSingletonClass : INotifyPropertyChanged
        {
          private string _companyName;
          
          public string CompanyName
          {
            get { return _companyName; }
            set
            {
                _companyName = value;

                // raise property changed here

             } 
        }

        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public static YourSingletonClass  YourInstance;
            public Window1()
            {
                InitializeComponent();
            }
         }

    <Window x:Class="MyNamespace.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1"
            xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly">
        <Grid>
            <TextBlock Text="{Binding Source={x:Static local:Window1.YourInstance}, Path=CompanyName}" />
        </Grid>
    </Window>

    Of course, you could move the CompanyName property directly to Window1, and then you could just do:

    <Window x:Class="MyNamespace.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1"
            xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
            x:Name="root">
        <Grid>
            <TextBlock Text="{Binding ElementName=root, Path=CompanyName}" />
        </Grid>
    </Window>


  • Wednesday, November 11, 2009 6:11 PMBruCru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for the help. 

    Chris