2

Question: Is it possible to access a static variable from code behind to be used in XAML?

Reason: I want a single string variable to keep a menu name which will be used in different places (in code behind and also in XAML).

Example (code behind):

public partial class MainWindow : Window
{
    public static readonly string menuName = "MyMenu";

    ... other code ...
}

Example (XAML):

<MenuItem Header="... here I want my menuName to appear ..." />
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
  • 1
    have you checked this? http://stackoverflow.com/questions/6794274/setting-label-text-in-xaml-to-string-constant – TonyS Dec 19 '13 at 14:16

3 Answers3

3

for that you would need to instantiate your class in xaml once, then you can use the static member.

it would be better to create a separate class for static variables and load it in xaml in resources.

something like this

<Window.Resources>
        <!-- Create an instance of the  class called MyClass -->

        <my:MyClass x:Key="MyClass" />

</Window.Resources>

then use it as something like

<TextBox Text="{x:Static my:MyClass.MyProperty}" Width="500" Height="100" /> 

or

<TextBlock Text="{Binding Source={StaticResource MyClass},Path=MyProperty}" />

also see XAML Binding to static classes

How to bind in XAML to a static property?

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
1

You should add it to project Resource dictionary:

go to you project -> Properties -> Resources-> Add Resource Button

then you can use it in Xaml or code behind like that:

-- XAML---

  <MenuItem Header="{x:Static properties:Resources.menuName}" /> 

--- Code behind ----

Properties.Resources.menuName
makc
  • 2,569
  • 1
  • 18
  • 28
0

you can not do this for the simple reason that when you bind on your property you will get an infinite nested calls to MainWindow which will generate an 'System.StackOverflowException' you should use a container class like this

    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {

            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = this; 
            }


        }
        public  class  WindowMessagesManager
        {
            private static string _header;
            public static string Header1
            {
                get { return "My Header"; }
                set { _header = value; }
            }

        }

}
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prop="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <prop:WindowMessagesManager x:Key="window" ></prop:WindowMessagesManager>
        //you can  try to uncomment this and you will get an exception 
        <!--<prop:MainWindow x:Key="window"></prop:MainWindow>-->
    </Window.Resources>
    <Grid>
        <Menu>
        <MenuItem  Height="100" Width="100" Header="{Binding  Source={StaticResource ResourceKey=window}, Path=Header1}"></MenuItem>
        </Menu>
    </Grid>
</Window>
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47