Access to codebehind variable in XAML

How can I access a public variable that is in the Sample.xaml.cs file, for example asp.net <%=VariableName%> ?

+44
c # wpf silverlight xaml
Mar 20 '09 at 16:13
source share
4 answers

There are several 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 need to add it after XAML receives the parsing, you can use DynamicResource above instead of StaticResource .

  • Make a variable a property of something in your XAML. This usually works through a DataContext :

     myWindow.DataContext = myVariable; 

    or

     myWindow.MyProperty = myVariable; 

    After that, something in your XAML can access it through Binding :

     <TextBlock Text="{Binding Path=PropertyOfMyVariable}"/> 

    or

     <TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/> 
+61
Mar 20 '09 at 16:30
source share

For binding, if the DataContext not used, you can simply add this to the code constructor behind:

 this.DataContext = this; 

Using this, each property in the code becomes available for binding:

 <TextBlock Text="{Binding PropertyName}"/> 

Another way is to simply give the name to the root XAML element:

 x:Name="root" 

Since XAML is compiled as an incomplete code class, we can access each property by name:

 <TextBlock Text="{Binding ElementName="root" Path=PropertyName}"/> 

Note: access is available only for properties; not the fields. set; and get; or {Binding Mode = OneWay} . If OneWay binding is used, the underlying data must implement INotifyPropertyChanged.

+23
Nov 23 '09 at 10:11
source share

For fast and dirty Windows in WPF, I prefer to bind the DataContext window to the window itself; all of this can 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, notice the binding used in the DataContext the window, it says: "Set your data context for yourself." Two text blocks are attached to MyProperty1 and MyProperty2 , the event handler for the button will set these values, which automatically apply to the Text property of two text blocks, since the properties are dependency properties.

+9
Mar 21 '09 at 21:36
source share

It is also worth noting that the "binding" can only be set on the DependencyProperty of the DependencyObject. If you want to set the Non DependencyProperty property (for example, a normal property) for an object in XAML, then you will have to use the first method of using resources in the code for Robert.

+3
Aug 25 '10 at 15:44
source share



All Articles