How can I access the public variable which in Sample.xaml.cs file like asp.net <%=VariableName%>
?

- 1,577
- 6
- 17
- 32

- 878
- 3
- 11
- 19
-
2[This question may also be helpful](http://stackoverflow.com/questions/1481130/wpf-binding-to-local-variable) since it is a bit more recent ... – Brendan Oct 28 '12 at 17:32
5 Answers
There are a few ways to do this.
Add your variable as a resource from codebehind:
myWindow.Resources.Add("myResourceKey", myVariable);
Then you can access it from XAML:
<TextBlock Text="{StaticResource myResourceKey}"/>
If you have to add it after the XAML gets parsed, you can use a
DynamicResource
above instead ofStaticResource
.Make the variable a property of something in your XAML. Usually this works through the
DataContext
:myWindow.DataContext = myVariable;
or
myWindow.MyProperty = myVariable;
After this, anything in your XAML can access it through a
Binding
:<TextBlock Text="{Binding Path=PropertyOfMyVariable}"/>
or
<TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/>

- 11,650
- 4
- 40
- 35
For binding, if DataContext
is not in use, you can simply add this to the constructor of the code behind:
this.DataContext = this;
Using this, every property in the code becomes accessible to binding:
<TextBlock Text="{Binding PropertyName}"/>
Another way is to just give a name to the root element of the XAML:
x:Name="root"
Since the XAML is compiled as a partial class of the code-behind, we can access every property by name:
<TextBlock Text="{Binding ElementName="root" Path=PropertyName}"/>
Note: access is only available to properties; not to fields. set;
and get;
or {Binding Mode = OneWay}
are necessary. If OneWay binding is used, the underlying data should implement INotifyPropertyChanged.

- 1,819
- 2
- 23
- 29
-
1Thanks! This answer helped me out. I couldn't use the DataContext method because I'm creating a user control, and if I set the data context within my control, then my control won't be able to get the datacontext of its parents when I use it in applications. Binding directly to the root gave me the same result I was looking for and leaves my control's data context untouched. – Benny Jobigan Feb 14 '10 at 08:59
-
Thanks for "root" advice. It works despite properties of code-behind are not listed in visual binding designer which is bit confusing. – Konstantin Salavatov Jun 10 '11 at 12:17
For quick-and-dirty Windows in WPF, I prefer binding the DataContext of the Window to the window itself; this can all be done in XAML.
Window1.xaml
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource self}}"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding Path=MyProperty1}" />
<TextBlock Text="{Binding Path=MyProperty2}" />
<Button Content="Set Property Values" Click="Button_Click" />
</StackPanel>
</Window>
Window1.xaml.cs
public partial class Window1 : Window
{
public static readonly DependencyProperty MyProperty2Property =
DependencyProperty.Register("MyProperty2", typeof(string), typeof(Window1), new UIPropertyMetadata(string.Empty));
public static readonly DependencyProperty MyProperty1Property =
DependencyProperty.Register("MyProperty1", typeof(string), typeof(Window1), new UIPropertyMetadata(string.Empty));
public Window1()
{
InitializeComponent();
}
public string MyProperty1
{
get { return (string)GetValue(MyProperty1Property); }
set { SetValue(MyProperty1Property, value); }
}
public string MyProperty2
{
get { return (string)GetValue(MyProperty2Property); }
set { SetValue(MyProperty2Property, value); }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Set MyProperty1 and 2
this.MyProperty1 = "Hello";
this.MyProperty2 = "World";
}
}
In the above example, note the binding used in the DataContext
property on the Window, this says "Set your data context to yourself". The two text blocks are bound to MyProperty1
and MyProperty2
, the event handler for the button will set these values, which will automatically propagate to the Text
property of the two TextBlocks as the properties are Dependency Properties.

- 2,283
- 20
- 22
It is also worth noting that a 'Binding' can only be set on a DependencyProperty of a DependencyObject. If you want to set a non DependencyProperty (eg. a normal property) on an object in XAML, then you will have to use Robert's first method of using resources in the code behind.

- 68,826
- 24
- 143
- 183
myWindow.xaml
<Window
...
<TextBlock Text="{ Binding Path=testString }" />
</Window>
myWindow.xaml.cs
public partial class myWindow: Window
{
public string testString { get; set; } = "This is a test string";
public myWindow()
{
DataContext = this;
InitializeComponent();
}
}
Important
- Set
Datacontext
testString
MUST bepublic
testString
MUST be aproperty
(have aget
andset
)

- 954
- 2
- 9
- 21