question

tstephansen-newmind avatar image
0 Votes"
tstephansen-newmind asked HuiLiu-MSFT commented

Opening a Popup throws exception

We have a dependency property on a custom control that is called OnChannelOneAndTwoPopupIsOpen and it has a property changed callback. In this callback the ChannelOneAndTwoPopup.IsOpen property is set to true. Sometimes when this value changes to true our application crashes with the exception message "The root Visual of a VisualTarget cannot have a parent." The code is below for reference.

Does anyone know how to fix this? All we are doing is setting the popup's IsOpen property to true. I haven't been able to reproduce it but it keeps happening to one of our customers and I'd like to be able to figure this out and get it fixed. Any help would be greatly appreciated.

 public static readonly DependencyProperty ChannelOneAndTwoPopupIsOpenProperty = DependencyProperty.Register(
             "ChannelOneAndTwoPopupIsOpen", typeof(bool), typeof(SessionTrendChart),
             new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                 OnChannelOneAndTwoPopupIsOpenChanged));
    
 public bool ChannelOneAndTwoPopupIsOpen
 {
     get => (bool) GetValue(ChannelOneAndTwoPopupIsOpenProperty);
     set => SetValue(ChannelOneAndTwoPopupIsOpenProperty, value);
 }
    
 private static void OnChannelOneAndTwoPopupIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     if (e.OldValue == e.NewValue) return;
     if (d is SessionTrendChart control && e.NewValue is bool channelOneAndTwoPopupIsOpen)
         control.OnChannelOneAndTwoPopupIsOpenChanged(channelOneAndTwoPopupIsOpen);
 }
    
 protected virtual void OnChannelOneAndTwoPopupIsOpenChanged(bool channelOneAndTwoPopupIsOpen)
 {
     if (ChannelOneAndTwoFiltersPopup == null || ChannelThreeAndFourFiltersPopup == null)
         return;
     if (channelOneAndTwoPopupIsOpen)
     {
         ChannelOneAndTwoFiltersPopup.IsOpen = true;
         ChannelThreeAndFourFiltersPopup.IsOpen = false;
     }
 }
windows-wpf
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@ tstephansen-newmind. What is the definition of ChannelOneAndTwoFiltersPopup and ChannelThreeAndFourFiltersPopup? Could you show me the code of the custom control OnChannelOneAndTwoPopupIsOpen? Is there a screenshot of the detailed error message?

0 Votes 0 ·

Here's the XAML for the popup. ChannelOneAndTwoFiltersPopup is the same as ChannelThreeAndFourFiltersPopup only with different content.

 <Popup x:Name="PART_ChannelOneAndTwoFiltersPopup"
                Grid.Row="1"
                AllowsTransparency="True"
                IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ChannelOneAndTwoPopupIsOpen}"
                Placement="Bottom"
                PlacementTarget="{Binding ElementName=FiltersHolder}"
                StaysOpen="True"
                HorizontalOffset="-324"
                VerticalOffset="8">

They are defined in the custom control as:

 public Popup ChannelOneAndTwoFiltersPopup;
 public Popup ChannelThreeAndFourFiltersPopup;

I don't have a screenshot of the error message because I can't reproduce it. Here's a screenshot of the StackTrace from AppCenter though.

stacktrace.png


0 Votes 0 ·

Hi,@ tstephansen-newmind. How do you display your custom Control? Do you have code similar to the code in this link?

0 Votes 0 ·
Show more comments

0 Answers