How can I access the public variable which in Sample.xaml.cs file like asp.net <%=VariableName%>?
<%=VariableName%>
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 of StaticResource.
DynamicResource
StaticResource
Make the variable a property of something in your XAML. Usually this works through the DataContext:
DataContext
myWindow.DataContext = myVariable;
or
myWindow.MyProperty = myVariable;
After this, anything in your XAML can access it through a Binding:
Binding
<TextBlock Text="{Binding Path=PropertyOfMyVariable}"/>
<TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/>
1.4m articles
1.4m replys
5 comments
56.9k users