pfinferno
pfinferno

Reputation: 1945

Binding code-behind variable to text in textbox in main window

I'm having issues binding text entered in a textbox to a variable in code behind.

Here's the xaml code for the textbox located in the main window:

<TextBox x:Name="Rotate1" Text="{Binding ElementName=this, Path=testvalue}" />

and in the code behind in main window:

private int testvalue { get; set;}

I know if it's the other way around I would have to update the source trigger on any change, but not sure what to do when it's changing variable to whatever the entered text is.

Upvotes: 0

Views: 816

Answers (1)

try in code:

public partial class MainWindow : Window
{
  public DependencyProperty TestValueProperty = DependencyProperty.Register("testvalue", typeof(int), typeof(MainWindow));
  public int testvalue
  {
    get { return (int)GetValue(TestValueProperty); }
    set
    {
      SetValue(TestValueProperty, value);
    }
  }
  public MainWindow()
  {
    InitializeComponent();
    testvalue = 6;
  }
}

in XAML

<Window x:Class="WpfApplication1.MainWindow"
    x:Name="thisForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <TextBox Text="{Binding ElementName=thisForm, Path=testvalue}" />
</Window>

UPD: oh! of course! Remove Tag in CS and XAML code

Upvotes: 1

Related Questions