Hi, I've just created a trivial xaml window that databinds a textbox to a property:
<Window x:Class="WpfTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="root">
<StackPanel>
<DockPanel>
<Button DockPanel.Dock="Right" Height="23" Click="buttonTest_Click">Browse...</Button>
<TextBox DockPanel.Dock="Left"
Height="23" Margin="0,0,2,0" Name="textBoxFileName"
Text="{Binding ElementName=root, Mode=TwoWay, Path=FileName}"/>
</DockPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding ElementName=root, Mode=TwoWay, Path=FileName}"></TextBlock>
<Button DockPanel.Dock="Bottom" Name="buttonToggle" Click="buttonToggle_Click">Toggle...</Button>
</DockPanel>
</StackPanel>
</Window>
And the (trivial) code behind:
namespace WpfTests
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(MainWindow));
public string FileName
{
get { return (string)GetValue(FileNameProperty); }
set { SetValue(FileNameProperty, value); }
}
private void buttonTest\_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog();
if (d.ShowDialog() == true)
this.FileName = d.FileName;
}
private void buttonToggle\_Click(object sender, RoutedEventArgs e)
{
this.FileName = "Toggle clicked.";
}
}
}
I have a couple of questions (that require simple 'assume-I-know-nothing' answers): To date, this is the only trivial example I've found of binding the value of a control to a class property. Am I doing this the correct way, or is this something fundamental to databinding that I'm missing? If, in VS2010, I go to the xaml view, select the 'Text' property of 'textBoxFileName' a