Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 341525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:43:02+00:00 2026-05-12T10:43:02+00:00

I have this XAML: <Window x:Class=WpfBindToCodeBehind.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 Loaded=Window_Loaded> <StackPanel Orientation=Vertical>

  • 0

I have this XAML:

<Window x:Class="WpfBindToCodeBehind.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"
    Loaded="Window_Loaded">
    <StackPanel Orientation="Vertical">
        <Button Name="ToggleExpand" Click="ToggleExpand_Click">Toggle Expander</Button>
        <Expander Name="Expander"
                  Header="Don't click me, click the button!"
                  IsExpanded="{Binding RelativeSource={RelativeSource Self},Path=MayExpand}">
            <TextBlock Text="{Binding}"/>
        </Expander>
    </StackPanel>
</Window>

This is the code behind:

public partial class Window1 : Window,INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public Window1()
        {
            InitializeComponent();
        }

        private void ToggleExpand_Click(object sender, RoutedEventArgs e)
        {
            MayExpand = !mayExpand;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Expander.DataContext = "Show me";
        }

        private bool mayExpand;
        public bool MayExpand
        {
            get { return mayExpand; }
            set
            {
                mayExpand = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MayExpand"));
            }
        }
    }

The Binding expression for the IsExpanded property is not working. This code is a simplification, in reality the expander’s binding is already set through the datacontent of a parent control.
How can I bind the IsExpanded property to a property of the code behind?
Can I bind it to the result of a method in the code behind?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:43:02+00:00Added an answer on May 12, 2026 at 10:43 am

    The source for the binding is a RelativeSource.Self. That means the source is the Expander rather than the Window. Something like this will work:

    IsExpanded="{Binding MayExpand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}"
    

    You could also use a name to simplify things:

    <Window x:Name="_root">
        <Expander IsExpanded="{Binding MayExpand, ElementName=_root}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code to reproduce this issue: xaml: <Window x:Class=WpfApplication1.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have this custom wpf user control: ShowCustomer.xaml: <UserControl x:Class=TestControlUpdate2343.Controls.ShowCustomer xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <Grid> <TextBlock
Im having some problems with binding in wpf/xaml. Have this simple file: <Window x:Class=test.Window1
I have encountered something very strange, simple WPF application <Window x:Class=ListBoxSelection.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow
In my application (WPF) i have this window: public partial class Window1 : Window
Suppose I have some XAML like this: <Window.Resources> <v:MyClass x:Key=whatever Text=foo\nbar /> </Window.Resources> Obviously
I am puzzled by this: I have made a very simple example: MainWindow.xaml: <Window
I have been trying to reproduce this example: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx For some reason my treeview
I have this Xaml <SolidColorBrush x:Key={x:Static SystemColors.HighlightBrushKey} Value=#123456/> <SolidColorBrush x:Key={x:Static SystemColors.HighlightTextBrushKey} Value=White/> THe first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.