chimata
chimata

Reputation: 1

Freezing main window after adding some items from childwindow

I am working on Silverlight 4 with mvvm and WCF services.

Whenever I am adding items from child window to main window. But at the same time main window automatically going to disabling mode. I think main window automatically freezes.

ChildWindow ViewModel

    public class AddFormFieldInformationViewModel : ViewModelBase
{


 private FieldInformationViewModel _FieldInformationViewModel;
    public FieldInformationViewModel FieldInformationViewModel
    {
        get { return _FieldInformationViewModel; }
        set
        {
            _FieldInformationViewModel = value;
            RaisePropertyChanged("FieldInformationViewModel");
        }
    }
 public void MoveSave(object obj)
    { this.FieldInformationViewModel.SelectedFormFields = FieldInformationModel;
                            ResultHandler(true);
 }

public Action ResultHandler { get; set; } }

ChildWindow .xaml.cs file

public partial class AddExistingFormFieldCategoryView : ChildWindow
{
    private AddFormFieldInformationViewModel vm;
    public AddExistingFormFieldCategoryView()
    {
        InitializeComponent();
         vm = new AddFormFieldInformationViewModel();
        this.DataContext = vm;
        vm.ResultHandler = result => { if (result) { Close(); } };

    }        
}

Main Window ViewModel

public class FieldInformationViewModel : ViewModelBase
{   private void executeOpenChildWindow(object parameter)
    {
        AddExistingFormFieldCategoryView cw = new AddExistingFormFieldCategoryView();
        ((AddFormFieldInformationViewModel)cw.DataContext).FieldInformationViewModel = this;
        cw.Show();
    }
}

After adding items from child window into main window, sometimes my main window is automatically freezing.

Upvotes: 0

Views: 462

Answers (2)

Chui Tey
Chui Tey

Reputation: 5574

You can subclass the ChildWindow and explicitly set the RootVisual to enabled. Source

using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DST_Common_Silverlight_Controls
{
    /// <summary>
    /// Bug in ChildWindow sometimes leaves app disabled. 
    /// </summary>
    public class ChildWindowEx : ChildWindow
    {
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
        }
    }
}

Then just use the new type instead of ChildWindow in xaml like this:

<slcommon:ChildWindowEx 
    x:Class="DST.AvSyncMonitor.Silverlight.Gui.ErrorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:slcommon="clr-namespace:DST_Common_Silverlight_Controls;assembly=DST.Common.Silverlight.Controls"
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"    
    Title="Error">

    <Grid x:Name="LayoutRoot" Width="540">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock x:Name="IntroductoryText" Grid.Row="0" Margin="0" 
             Text="An unknown error was encountered. Please contact your administrator for more information."/>

        <StackPanel x:Name="ContentStackPanel" Grid.Row="2" Margin="0,6,0,0">

            <TextBlock x:Name="LabelText" TextWrapping="Wrap" Margin="0,0,0,2" 
                     Text="Error details"/>
            <TextBox x:Name="ErrorTextBox" Height="90" TextWrapping="Wrap" IsReadOnly="True"
                     VerticalScrollBarVisibility="Auto"/>

        </StackPanel>

        <Button x:Name="OKButton" Grid.Row="3" Click="OKButton_Click" 
            Width="75" Height="23" HorizontalAlignment="Right" Margin="0,10,0,0" 
            TabIndex="0" Content="OK"/>

    </Grid>

</slcommon:ChildWindowEx>

Upvotes: 0

Stainedart
Stainedart

Reputation: 1979

Hi this is a bug you are experiencing see the sample below. I the same issue and you have to manually enable the display of the main page:

        private static void ShowError(string message, string details)
        {
            ErrorWindow error = new ErrorWindow(message, details);
            error.Closed += new EventHandler(error_Closed);
            error.Show();
        }

        static void error_Closed(object sender, EventArgs e)
        {
            Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
        }

Upvotes: 2

Related Questions