locked
binding to property of Window is not working RRS feed

  • Question

  • I would like to bind to properties of the Window class.  Is this doable?  How to get such binding to work?

    What I do is assign an x:Name name to the Window control. Then use  ElementName and Path to bind to the property of the window.  But this does not work. I do not get a binding error. But the TextBlock in my sample shows as blank.  

    Why is binding not working?

    thanks,

    <Window x:Class="wpfDemo.steve7"
            x:Name="windowMain"
            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:wpfDemo"
            mc:Ignorable="d"
            Title="steve7" Height="300" Width="300">
      <StackPanel x:Name="stackMain" Orientation="Vertical">
        <TextBlock Text="{Binding ElementName=windowMain, Path=ZipCode}"/>
        <TextBlock Text="{Binding Path=ZipCode}" Background="AntiqueWhite"/>
      </StackPanel>  
    </Window>
    
      public partial class steve7 : Window
      {
        public string ZipCode
        { set; get; }
        public steve7()
        {
          InitializeComponent();
          this.ZipCode = "11210";
          this.Loaded += Steve7_Loaded;
        }
    
        private void Steve7_Loaded(object sender, RoutedEventArgs e)
        {
          this.stackMain.DataContext = this;
        }
      }
    
    

    Saturday, July 2, 2016 2:57 AM

Answers

  • >>Why is binding not working?

    Instead of binding to an ElementName, you could bind to the window using a RelativeSource. Then it works:

    <TextBlock Text="{Binding Path=ZipCode, RelativeSource={RelativeSource AncestorType=Window}}"/>
    <TextBlock Text="{Binding Path=ZipCode}" Background="AntiqueWhite"/>

    The reason why binding to an ElementName doesn't work in this case is because of name scopes. The ElementName binding looks directly to the window's namescope for a registered object with that name and the Window base class ha no "ZipCode" property. Please refer to the following thread for more information: http://stackoverflow.com/questions/4317097/elementname-vs-relativeresource

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Steve Richter Saturday, July 2, 2016 12:17 PM
    Saturday, July 2, 2016 11:00 AM

All replies

  • this works when the property on the Window class is a dependency property.  Honestly, I do not understand dependency properties. I know I need to use them if I want to bind to the property of a user control. But in this case I could bind to the regular property thru the data context ( 2nd textBlock binding. )

    <Window x:Class="wpfDemo.steve7"
            x:Name="window7"
            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:wpfDemo"
            mc:Ignorable="d"
            Title="steve7" Height="300" Width="300">
      <StackPanel x:Name="stackMain" Orientation="Vertical">
        <TextBlock Text="{Binding ElementName=window7, Path=ZipCode}"/>
        <TextBlock Text="{Binding Path=ZipCode}" Background="AntiqueWhite"/>
        <TextBlock Text="{Binding Path=WindowName}"/>
      </StackPanel>  
    </Window>
    
    
      public partial class steve7 : Window
      {
    
        public string WindowName
        { get; set; }
    
        public string ZipCode
        {
          get { return (string)GetValue(ZipCodeProperty); }
          set { SetValue(ZipCodeProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ZipCode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ZipCodeProperty =
            DependencyProperty.Register("ZipCode", typeof(string), typeof(steve7), new PropertyMetadata(null));
    
        public steve7()
        {
          InitializeComponent();
          this.ZipCode = "11210";
          this.Loaded += Steve7_Loaded;
        }
    
        private void Steve7_Loaded(object sender, RoutedEventArgs e)
        {
          this.stackMain.DataContext = this;
          this.WindowName = this.Name;
        }
      }
    

    Saturday, July 2, 2016 8:51 AM
  • >>Why is binding not working?

    Instead of binding to an ElementName, you could bind to the window using a RelativeSource. Then it works:

    <TextBlock Text="{Binding Path=ZipCode, RelativeSource={RelativeSource AncestorType=Window}}"/>
    <TextBlock Text="{Binding Path=ZipCode}" Background="AntiqueWhite"/>

    The reason why binding to an ElementName doesn't work in this case is because of name scopes. The ElementName binding looks directly to the window's namescope for a registered object with that name and the Window base class ha no "ZipCode" property. Please refer to the following thread for more information: http://stackoverflow.com/questions/4317097/elementname-vs-relativeresource

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Steve Richter Saturday, July 2, 2016 12:17 PM
    Saturday, July 2, 2016 11:00 AM