locked
How to bind a value? RRS feed

  • Question

  • I declare property in Window1.xaml.cs  file. How can i bind it ? as i am new WPF . I dont know the exact XAML and C# code for this. Could you share with me?
    Friday, June 4, 2010 8:33 AM

Answers

  • You can use below code to access property of your window.

    XAML Code

    <Window x:Class="WpfApplication2.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300" x:Name="myWindow" >
      <StackPanel>
        <TextBlock Text="OK"></TextBlock>
         <TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/>
      </StackPanel>

    CS Code :

        public static DependencyProperty MyPropertyProperty = DependencyProperty.Register(
              "MyProperty", typeof(string), typeof(Window1));
    
    
        public string MyProperty
        {
          get
          {
            return (string)GetValue(MyPropertyProperty);
          }
          set
          {
            SetValue(MyPropertyProperty, value);
          }
    
        }
    

    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Mike Danes Friday, June 4, 2010 9:23 AM
    • Marked as answer by David alice DD Friday, June 4, 2010 10:26 AM
    Friday, June 4, 2010 9:14 AM
  • The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object. 

    I would suggest that if you are making a custom control or markup extension, you generally want to expose any of its public properties as dependency properties so that the consumer of your control can better manipulate the settings in XAML (without having to do it in code-behind). 

    If your property will commonly be the source of a databinding (e.g. providing the Text for a TextBlock), I would recommend using a standard CLR property and having the containing class implement INotifyPropertyChanged.

    Friday, June 4, 2010 10:23 AM

All replies

  • You can use below code to access property of your window.

    XAML Code

    <Window x:Class="WpfApplication2.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300" x:Name="myWindow" >
      <StackPanel>
        <TextBlock Text="OK"></TextBlock>
         <TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/>
      </StackPanel>

    CS Code :

        public static DependencyProperty MyPropertyProperty = DependencyProperty.Register(
              "MyProperty", typeof(string), typeof(Window1));
    
    
        public string MyProperty
        {
          get
          {
            return (string)GetValue(MyPropertyProperty);
          }
          set
          {
            SetValue(MyPropertyProperty, value);
          }
    
        }
    

    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Mike Danes Friday, June 4, 2010 9:23 AM
    • Marked as answer by David alice DD Friday, June 4, 2010 10:26 AM
    Friday, June 4, 2010 9:14 AM
  • Hi ,

    If i use ordinay property instead of DependencyProperty it does not work . Why ? What is the reason?

    Friday, June 4, 2010 10:07 AM
  • The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object. 

    I would suggest that if you are making a custom control or markup extension, you generally want to expose any of its public properties as dependency properties so that the consumer of your control can better manipulate the settings in XAML (without having to do it in code-behind). 

    If your property will commonly be the source of a databinding (e.g. providing the Text for a TextBlock), I would recommend using a standard CLR property and having the containing class implement INotifyPropertyChanged.

    Friday, June 4, 2010 10:23 AM