Click here to Skip to main content
15,867,939 members
Home / Discussions / WPF
   

WPF

 
Questionc# Spotify API not returning correctly. Pin
elfenliedtopfan52-Jun-23 18:03
elfenliedtopfan52-Jun-23 18:03 
AnswerRe: c# Spotify API not returning correctly. Pin
Pete O'Hanlon4-Jun-23 8:42
subeditorPete O'Hanlon4-Jun-23 8:42 
AnswerRe: c# Spotify API not returning correctly. Pin
jschell5-Jun-23 5:01
jschell5-Jun-23 5:01 
QuestionNeed help please regarding your awesome WPF MultiComboBox project :) Pin
Member 1160463425-May-23 5:21
Member 1160463425-May-23 5:21 
AnswerRe: Need help please regarding your awesome WPF MultiComboBox project :) Pin
jeron125-May-23 5:50
jeron125-May-23 5:50 
AnswerRe: Need help please regarding your awesome WPF MultiComboBox project :) Pin
Richard Deeming25-May-23 21:28
mveRichard Deeming25-May-23 21:28 
QuestionNavigationControl - Part 3 Pin
Kevin Marois14-May-23 8:01
professionalKevin Marois14-May-23 8:01 
AnswerRe: NavigationControl - Part 3 Pin
Richard Deeming14-May-23 21:40
mveRichard Deeming14-May-23 21:40 
Problem 1:
The change event for the IsPaneExpanded property sets the IsPaneExpanded property to false if _isInitialized is false. If the property has just changed to true, that will cause the change event to fire again. As a result, the _isPaneExpanded field will be set to true, then immediately set back to false.

Thankfully, WPF won't fire the event if the property is set to the same value; otherwise, you'd end up with infinite recursion.

It would probably be better to use a CoerceValueCallback[^] instead:
C#
public static readonly DependencyProperty IsPaneExpandedProperty =
    DependencyProperty.Register("IsPaneExpanded",
        typeof(bool),
        typeof(NavigationPane),
        new PropertyMetadata(
            defaultValue: false, 
            propertyChangedCallback: new PropertyChangedCallback(OnIsPaneExpandedChanged),
            coerceValueCallback: new CoerceValueCallback(CoerceIsPaneExpanded)
        ));

private static void OnIsPaneExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = (NavigationPane)d;
    control._isPaneExpanded = (bool)e.NewValue;
    control.CoerceValue(IsPaneExpandedProperty);
    
    if (control._isInitialized && control._isPaneExpanded)
    {
        _ = control.Load();
    }
}

private static object CoerceIsPaneExpanded(DependencyObject d, object baseValue)
{
    var control = (NavigationPane)d;
    return control._isInitialized ? baseValue : (object)false;
}

Problem 2:
You need to change the _isInitialized field, trigger the change event for the IsPaneExpanded property, then load the data.
C#
private void NavigationPane_Initialized(object? sender, EventArgs e)
{
    _isInitialized = true;
    CoerceValue(IsPaneExpandedProperty);
    
    if (_isPaneExpanded)
    {
        _ = Load();
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: NavigationControl - Part 3 Pin
Kevin Marois15-May-23 17:14
professionalKevin Marois15-May-23 17:14 
QuestionBubbling Event Question Pin
Kevin Marois9-May-23 14:32
professionalKevin Marois9-May-23 14:32 
AnswerRe: Bubbling Event Question Pin
Gerry Schmitz12-May-23 5:28
mveGerry Schmitz12-May-23 5:28 
GeneralRe: Bubbling Event Question Pin
Kevin Marois12-May-23 5:35
professionalKevin Marois12-May-23 5:35 
GeneralRe: Bubbling Event Question Pin
Gerry Schmitz12-May-23 5:46
mveGerry Schmitz12-May-23 5:46 
GeneralRe: Bubbling Event Question Pin
Kevin Marois12-May-23 6:16
professionalKevin Marois12-May-23 6:16 
AnswerRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 9:55
mvaKenneth Haugland14-May-23 9:55 
GeneralRe: Bubbling Event Question Pin
Kevin Marois14-May-23 10:40
professionalKevin Marois14-May-23 10:40 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 11:06
mvaKenneth Haugland14-May-23 11:06 
GeneralRe: Bubbling Event Question Pin
Kevin Marois14-May-23 11:08
professionalKevin Marois14-May-23 11:08 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 11:19
mvaKenneth Haugland14-May-23 11:19 
GeneralRe: Bubbling Event Question Pin
Kevin Marois15-May-23 17:47
professionalKevin Marois15-May-23 17:47 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland15-May-23 18:46
mvaKenneth Haugland15-May-23 18:46 
GeneralRe: Bubbling Event Question Pin
Kevin Marois16-May-23 7:02
professionalKevin Marois16-May-23 7:02 
QuestionExpander Header Content Stretch Pin
Kevin Marois9-May-23 11:25
professionalKevin Marois9-May-23 11:25 
AnswerRe: Expander Header Content Stretch Pin
Richard Deeming9-May-23 21:48
mveRichard Deeming9-May-23 21:48 
GeneralRe: Expander Header Content Stretch Pin
Kevin Marois13-May-23 6:05
professionalKevin Marois13-May-23 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.