How do I set the text of a TextBlock to a global variable?
- 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
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>- Marked As Answer byLinda LiuMSFT, ModeratorFriday, November 13, 2009 10:11 AM
All Replies
- 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.
- Unproposed As Answer byLinda LiuMSFT, ModeratorFriday, November 13, 2009 10:08 AM
- Proposed As Answer byTimmyGR Saturday, November 07, 2009 2:55 PM
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}" />- 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:I have declared a local namespace as:Text="{x:Static local:Window1.sCompany}"
where Balla_Accounting is the name of my project.xmlns:local="clr-namespace:Balla_Accounting"
Thanks for your help.
Chris - You need that static sComanyName to be declared as public.
public static sCompanyName as string = "MyCompany" - 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?
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>- Marked As Answer byLinda LiuMSFT, ModeratorFriday, November 13, 2009 10:11 AM
- Thanks for the help.
Chris

