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 8162165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:38:57+00:00 2026-06-06T18:38:57+00:00

I have a strange problem with my WPF control. Basically I have a control

  • 0

I have a strange problem with my WPF control. Basically I have a control which presents modal content in front of my primary content. My control is a modification of my answer shown here.

I have a Bool dependency property called IsShown which is used to hide / show the modal content. The property is defined like this:

public static readonly DependencyProperty IsShownProperty =
    = DependencyProperty.Register("IsShown",
    typeof(bool), 
    typeof(ModalContentControl), 
    new UIPropertyMetadata(false, IsShownChangedCallback));

It has a standard .Net property wrapper like so:

public bool IsShown
{
    get { return (bool)GetValue(IsShownProperty); }
    set { SetValue(IsShownProperty, value); }
}

The property changed call back looks like this:

private static void IsShownChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ModalContentControl control = (ModalContentControl)d;

    if ((bool)e.NewValue == true)
    {
        control.ShowModalContent();
    }
    else
    {
        control.HideModalContent();
    }
}

The ShowModalContent and HideModalContent methods contain the logic to show and hide the modal content respectively.

I have this control in my window and the IsShown property is bound to my viewModel. I can press a button which invokes a command that changes a boolean property that is bound to the IsShown property of my control. The modal content is shown and hidden as expected.

My problem comes from the fact that the ShowModalContent and HideModalContent methods are public so that they can be called from code. What I want to be able to do is call ShowModalContent, for example, and when I query the IsShown property it should report the correct value (in this case true) E.G. If I was using a regular property I would have a backing field of type Bool which would be updated in the method. This field would then be returned by the property get statement.

But because my property is a dependency property I don’t have access to a backing field. The only way I can update the value from the method is to set the dependency property through the .net wrapper but this stops my control from working (once the content is shown I cannot get rid of it again).

I thought this behaviour might be caused by setting the IsShown property in the method, which calls the property changed callback, which sets the IsShown property which calls.. but I remember reading somewhere that WPF protects against this scenario.

So how do I fix this problem so that my boolean dependency property reports the correct value?

  • 1 1 Answer
  • 0 Views
  • 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-06-06T18:38:58+00:00Added an answer on June 6, 2026 at 6:38 pm

    With a little bit of thought (and some luck) I managed to figure out what the problem was. There were 2 things I needed to change in order to get this to work.

    The first problem was that the ShowModalContent and HideModalContent methods were doing two things.

    1. Setting the IsShown property.
    2. Updating the state of the control.

    This was a problem because changing the IsShown property calls the IsChangedCallback which calls either the ShowModalContent and HideModalContent methods which then change the IsShown property. I think WPF stops an endless loop occurring but even trying to manage this in code by caching the values was a major headache.

    My moment of inspiration came when I realised that the only thing the public method should be doing is setting the property. My public methods now look like this:

    public void ShowModalContent()
    {
        IsShown = true;
    }
    
    public void HideModalContent()
    {
        IsShown = false;
    }
    

    This then triggers the property changed callback which updates the state of the control.

    My second moment of inspiration was when I realised my ViewModel wasn’t accurately reflecting the state of the IsShown property and that this was being caused by the fact that apparently the default dependency property binding mode is OneWay. I simply updated my DependencyProperty definitions binding mode to BindsTwoWayByDefault and everything now works fine.

    IsShownProperty = DependencyProperty.Register("IsShown",
        typeof(bool), typeof(ModalContentControl), 
        new FrameworkPropertyMetadata(false, 
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
            IsShownChangedCallback));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are facing a strange problem. We have a user control written in WPF
I have a strange problem with a WPF DataGrid from WPF Toolkit. The scrollbars
I have a strange WPF/XAML problem. By default, I want all of the nodes
I have a problem with bindings in wpf. I've discovered a strange problem. I
I have some strange problem in my WPF app. I'm using a MVVM pattern
I have strange problem for which I can't think of a solution. I have
I have strange problem. I have class which behaves similar dropdown list. package test.view;
Yet another strange WPF error: I have a custom control in a simple XAML
I have a really strange problem with WPF. A simple window with a button
i have strange problem doing reporting: i have numerous clients with different issued invoices.

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.