I can't seem to bind a control's value to an object. I want to bind a TextBox
to a string
object, the idea is that when textbox's text changes, it should automatically change the object as well. couldn't figure out what I'm doing wrong. Here is what I have tried:
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
string str;
public MainWindow()
{
InitializeComponent();
this.DataContext = str;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
and MainWindow.xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="150">
<Grid Margin="0,0,642,319">
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="{Binding str}" VerticalAlignment="Top" Width="120" Margin="0,0,-120,-46" />
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click" Height="23" Margin="0,28,-75,-51" RenderTransformOrigin="0.423,2.257" />
</Grid>
</Window>
So, when I enter something to the textbox and click the button, I should see the text in str
while debugging but it is always null