Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: ChildWindow changes

$
0
0
Hi Guys!

I’m having a similar problem with the WindowContainer/ChildWindow.
Here is a little sample snipped:
<Grid>
    <Grid>
        <TextBlock Text="MainContent" />
    </Grid>
    <xctk:WindowContainer ModalBackgroundBrush="LightGray">
        <xctk:ChildWindow Width="480"
                          Height="320"
                          IsModal="True"
                          Visibility="Collapsed"
                          WindowState="Closed">
            <TextBlock Text="Test" />
        </xctk:ChildWindow>
    </xctk:WindowContainer>
</Grid>
When starting the application, the WindowContainer is in “modal”-mode and therefore blocks the content of my MainContent grid. This happens although there is no window visible. My only window has a WindowState of Closed and the Visibility is set to Collapsed as well.

So after I start the application, the following happens:
  1. WindowContainer is created
  2. ChildWindow is created (Visibility=Visible, IsVisible=false)
  3. WindowContainer. OnVisualChildrenChanged is called and events are hooked up
  4. ChildWindow.IsModel is set to true, which triggers the IsModalChanged in the WindowContainer class. This in turn calls WindowContainer. SetModalBackground method. At this time we have a ChildWindow that is Visible (Visibility, but not IsVisible)!
  5. ChildWindow.Visibility is set to Collapsed. Although the Visibility of the ChildWindow has changed, the IsVisibleChanged event is not triggered, because IsVisible was never true.
The fix is rather simple. Since I include the WPF Toolkit via NuGet, I want stick with the original source. So I created my one ChildWindow class:
public class CustomChildWindow : ChildWindow
{
    static CustomChildWindow()
    {
        ChildWindow.WindowStateProperty.OverrideMetadata(typeof(CustomChildWindow), new FrameworkPropertyMetadata(Xceed.Wpf.Toolkit.WindowState.Closed, null, OnWindowStateChanged));
    }
    
    public CustomChildWindow()
    {
        Visibility = System.Windows.Visibility.Collapsed;
    }
    
    private static object OnWindowStateChanged(DependencyObject d, object baseValue)
    {
        var window = d as CustomChildWindow;
        if (window != null && baseValue != null)
        {
            window.Visibility = (Xceed.Wpf.Toolkit.WindowState)baseValue == Xceed.Wpf.Toolkit.WindowState.Open ? Visibility.Visible : Visibility.Collapsed;
        }
        return baseValue;
    }
}
I did not much of testing but didn’t recognise any side-effects yet. :)

Viewing all articles
Browse latest Browse all 4964

Trending Articles