Trivial data binding to member variables.
-
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
-
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
Hi Barry, This is by design. Designer never look for the code behind of your current class. If you want provide designer support, move all your property declaration from your current class to another class and inherit to your current class. For example, create extended window with your property like below mentioned.
public class ExtendedWindow : Window { public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(ExtendedWindow)); public string FileName { get { return (string)GetValue(FileNameProperty); } set { SetValue(FileNameProperty, value); } } }
Now inherit this extended class to your MyWindow classpublic partial class MainWindow : ExtendedWindow { public MainWindow() { InitializeComponent(); } 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."; } }
Xaml<local:ExtendedWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
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>
-
Hi Barry, This is by design. Designer never look for the code behind of your current class. If you want provide designer support, move all your property declaration from your current class to another class and inherit to your current class. For example, create extended window with your property like below mentioned.
public class ExtendedWindow : Window { public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(ExtendedWindow)); public string FileName { get { return (string)GetValue(FileNameProperty); } set { SetValue(FileNameProperty, value); } } }
Now inherit this extended class to your MyWindow classpublic partial class MainWindow : ExtendedWindow { public MainWindow() { InitializeComponent(); } 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."; } }
Xaml<local:ExtendedWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
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>
Venugopalm - thanks for the reply, this is helpful!
Regards, Barry