Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Trivial data binding to member variables.

Trivial data binding to member variables.

Scheduled Pinned Locked Moved WPF
wpfwcfcomtutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Barry Lapthorn
    wrote on last edited by
    #1

    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

    V 1 Reply Last reply
    0
    • B Barry Lapthorn

      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

      V Offline
      V Offline
      venugopalm
      wrote on last edited by
      #2

      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 class public 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>
      
      B 1 Reply Last reply
      0
      • V venugopalm

        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 class public 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>
        
        B Offline
        B Offline
        Barry Lapthorn
        wrote on last edited by
        #3

        Venugopalm - thanks for the reply, this is helpful!

        Regards, Barry

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups