Skip to content

[BUG] Fix Expander in Popup bug#422

Open
mou-haz wants to merge 4 commits intoiNKORE-NET:mainfrom
mou-haz:ExpanderInPopupBug
Open

[BUG] Fix Expander in Popup bug#422
mou-haz wants to merge 4 commits intoiNKORE-NET:mainfrom
mou-haz:ExpanderInPopupBug

Conversation

@mou-haz
Copy link
Contributor

@mou-haz mou-haz commented Jan 26, 2026

Fix #421

@mou-haz mou-haz force-pushed the ExpanderInPopupBug branch 2 times, most recently from 5d48ddd to bfabfa4 Compare January 26, 2026 14:09
@mou-haz mou-haz force-pushed the ExpanderInPopupBug branch from bfabfa4 to 4dddcd5 Compare January 26, 2026 14:18
@NotYoojun
Copy link
Member

Not sure adding CachedToAnimateControlProperty is the best practice.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a NullReferenceException that occurred when displaying a ContentDialog containing an Expander with the ExpanderCardStyleKey style. The issue stemmed from the animation helper attempting to access a template part before the template was applied.

Changes:

  • Adds ApplyTemplate() call to ensure the Expander's template is loaded before searching for animation target parts
  • Implements caching mechanism to store the found animation target control and avoid repeated template searches
  • Replaces silent null return with a descriptive exception when the animation target part cannot be found

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


expander.ApplyTemplate();
var toAnimateControl = expander.Template?.FindName(GetToAnimateControlName(expander), expander) as FrameworkElement ??
throw new ArgumentNullException("ToAnimateControl", $"Couldn't find the part to animate either update the ExpanderAnimationsHelper.ToAnimateControlName or rename the animation target part to default: {DefaultAnimationTargetPartName}");
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception type should be InvalidOperationException rather than ArgumentNullException. An ArgumentNullException is used when a method parameter is null, but in this case, the issue is that a required control part cannot be found in the template, which is an invalid state or operation issue, not a null argument issue.

Suggested change
throw new ArgumentNullException("ToAnimateControl", $"Couldn't find the part to animate either update the ExpanderAnimationsHelper.ToAnimateControlName or rename the animation target part to default: {DefaultAnimationTargetPartName}");
throw new InvalidOperationException($"Couldn't find the part to animate either update the ExpanderAnimationsHelper.ToAnimateControlName or rename the animation target part to default: {DefaultAnimationTargetPartName}");

Copilot uses AI. Check for mistakes.
Comment on lines 38 to 43
private static readonly DependencyProperty CachedToAnimateControlProperty =
DependencyProperty.RegisterAttached(
"CachedToAnimateControl",
typeof(FrameworkElement),
typeof(ExpanderAnimationsHelper),
new PropertyMetadata(null));
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caching mechanism doesn't account for runtime template changes. If an Expander's Template property is changed after it has been loaded, the cached control will still reference the old template's element. Consider either: 1) Listening to the TemplateApplied event or overriding OnApplyTemplate to clear the cache, 2) Adding a property changed callback on Control.TemplateProperty to clear the cache, or 3) Accepting this as a known limitation if runtime template changes are not a supported scenario.

Copilot uses AI. Check for mistakes.

expander.ApplyTemplate();
var toAnimateControl = expander.Template?.FindName(GetToAnimateControlName(expander), expander) as FrameworkElement ??
throw new ArgumentNullException("ToAnimateControl", $"Couldn't find the part to animate either update the ExpanderAnimationsHelper.ToAnimateControlName or rename the animation target part to default: {DefaultAnimationTargetPartName}");
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message could be clearer. Consider rephrasing to: "Couldn't find the part to animate. Either update ExpanderAnimationsHelper.ToAnimateControlName, or rename the animation target part to the default: {DefaultAnimationTargetPartName}"

Suggested change
throw new ArgumentNullException("ToAnimateControl", $"Couldn't find the part to animate either update the ExpanderAnimationsHelper.ToAnimateControlName or rename the animation target part to default: {DefaultAnimationTargetPartName}");
throw new ArgumentNullException("ToAnimateControl", $"Couldn't find the part to animate. Either update ExpanderAnimationsHelper.ToAnimateControlName, or rename the animation target part to the default: {DefaultAnimationTargetPartName}");

Copilot uses AI. Check for mistakes.
@mou-haz
Copy link
Contributor Author

mou-haz commented Feb 2, 2026

Hey @NotYoojun , i removed the caching property. can you see if it's good now ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Opening a content dialog with an expander with ExpanderCardStyleKey throws System.NullReferenceException

2 participants