Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p><strong>Window1.xaml</strong></p> <pre><code>&lt;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"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Path=MyProperty1}" /&gt; &lt;TextBlock Text="{Binding Path=MyProperty2}" /&gt; &lt;Button Content="Set Property Values" Click="Button_Click" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>Window1.xaml.cs</strong></p> <pre><code>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"; } } </code></pre> <p>In the above example, note the binding used in the <code>DataContext</code> property on the Window, this says "Set your data context to yourself". The two text blocks are bound to <code>MyProperty1</code> and <code>MyProperty2</code>, the event handler for the button will set these values, which will automatically propagate to the <code>Text</code> property of the two TextBlocks as the properties are Dependency Properties. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload