2011-11-21 7 views
0

새 작업을 추가하는 사용자 정의 컨트롤 (radwindow)이 있습니다. 부모 창에서이 창을 호출합니다. 그러나이 모달 컨트롤을 닫을 때 다시 호출 할 때 viewmodel은 이전과 동일하므로 이전 데이터로 컨트롤이 채워져 있습니다. Ofcource 나는 빈 칸으로 제어 할 필요가있다.ViewModel이 닫힌 후에도 계속 존재합니다.

나는 MVVM Light을 사용하고있다. 나는 그것이 Messenger와 관련이있을 수 있음을 알고 있지만, 코드에 아무런 문제가 없다고 생각합니다. 여기

<telerik:RadWindow x:Class="MyAppApp.View.AddMessageTemplateView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        xmlns:prismvm="clr-namespace:Microsoft.Practices.Prism.ViewModel;assembly=Microsoft.Practices.Prism" 
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4" 
        xmlns:Converters="clr-namespace:MyAppApp.Converters" 

        xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
        xmlns:HTMLParsing="clr-namespace:MyAppApp.Helpers.HTMLParsing" mc:Ignorable="d" 
      DataContext="{Binding AddMessageTemplate, Source={StaticResource Locator}}" d:DesignHeight="300" d:DesignWidth="400" WindowStartupLocation="CenterScreen" 
    ResizeMode="NoResize" Width="355" > 
    <telerik:RadWindow.Resources> 
     <Converters:RadioButtonConverter x:Key="RadioButtonConverter"/> 
     <Converters:EnumsMatchToVisibilityConverter x:Key="EnumsMatchToVisibilityConverter"/> 
    </telerik:RadWindow.Resources> 
    <Grid > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="40"/> 
     </Grid.RowDefinitions> 
     <TextBlock Text="{Binding Path=MyAppResources.label_PleaseSelectTypeOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/> 
     <StackPanel Grid.Row="1" Orientation="Horizontal"> 
      <RadioButton Content="Email" Margin="0 0 10 0" 
    GroupName="MessageTypes" 
    IsChecked="{Binding SelectedType, Mode=TwoWay, ConverterParameter=Email, 
      Converter={StaticResource RadioButtonConverter}}" /> 

      <RadioButton Content="SMS" Margin="0 0 10 0" 
    GroupName="MessageTypes" 
    IsChecked="{Binding SelectedType, Mode=TwoWay, ConverterParameter=Sms, 
      Converter={StaticResource RadioButtonConverter}}" /> 
     </StackPanel> 
     <!--Place for New Item> --> 
     <StackPanel Grid.Row="2" HorizontalAlignment="Stretch"> 
      <Grid 
    x:Name="DetailsGrid"> 
       <prismvm:DataTemplateSelector 
     Content="{Binding NewTemplate}" HorizontalContentAlignment="Stretch"> 
        <prismvm:DataTemplateSelector.Resources> 
         <DataTemplate 
       x:Key="EmailMessageTemplate"> 
          <StackPanel Orientation="Vertical"> 
           <TextBlock Text="{Binding Path=MyAppResources.label_NameOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/> 
           <TextBox Text="{Binding Name,Mode=TwoWay}" HorizontalAlignment="Stretch" /> 
           <TextBlock Text="{Binding Path=MyAppResources.label_SubjectOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/> 
           <TextBox Text="{Binding EmailSubject,Mode=TwoWay}" HorizontalAlignment="Stretch"/> 

           <StackPanel Orientation="Vertical"> 
            <RadioButton Content="HTML" Margin="0 0 10 0" 
    GroupName="EmailFormat" 
    IsChecked="{Binding EmailFormat, Mode=TwoWay, ConverterParameter=HTML, 
      Converter={StaticResource RadioButtonConverter}}" /> 

            <RadioButton Content="Plain Text" Margin="0 0 10 0" 
    GroupName="EmailFormat" x:Name="PlainTextRadioButton" 
    IsChecked="{Binding EmailFormat, Mode=TwoWay, ConverterParameter=PlainText, 
      Converter={StaticResource RadioButtonConverter}}" /> 
           </StackPanel> 
           <TextBlock Text="{Binding Path=MyAppResources.label_BodyOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/> 

           <ScrollViewer MaxHeight="100" HorizontalAlignment="Stretch" Margin="0,10,0,10" Name="scrollViewerEmailBody" VerticalAlignment="Top"> 
            <TextBox Text="{Binding Body,Mode=TwoWay}" TextWrapping="Wrap" AcceptsReturn="True" /> 
           </ScrollViewer> 

           <ScrollViewer HorizontalAlignment="Stretch" Margin="0,10,0,10" Name="scrollViewerEmail2RadHtmlPlaceholder" Visibility="{Binding EmailFormat,ConverterParameter=HTML,Converter={StaticResource EnumsMatchToVisibilityConverter}}" > 
            <HTMLParsing:HtmlTextBlock Html="{Binding Body}"/> 
           </ScrollViewer> 
          </StackPanel> 

         </DataTemplate> 
         <DataTemplate 
       x:Key="SmsMessageTemplate"> 
          <StackPanel Orientation="Vertical"> 
           <TextBlock Text="{Binding Path=MyAppResources.label_NameOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/> 
           <TextBox Text="{Binding Name,Mode=TwoWay}" HorizontalAlignment="Stretch" /> 
           <TextBlock Text="{Binding Path=MyAppResources.label_BodyOfMessageTemplate, Source={StaticResource ResourcesWrapper}}"/> 

           <ScrollViewer MaxHeight="100" HorizontalAlignment="Stretch" Margin="0,10,0,10" VerticalAlignment="Top"> 
            <TextBox Text="{Binding Body,Mode=TwoWay}" TextWrapping="Wrap" AcceptsReturn="True" /> 
           </ScrollViewer> 
          </StackPanel> 
         </DataTemplate> 

        </prismvm:DataTemplateSelector.Resources> 
       </prismvm:DataTemplateSelector> 
      </Grid> 
     </StackPanel> 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" 
       Grid.Row="3" Grid.Column="0"> 
      <telerik:RadButton x:Name="btnOK" Width="120" Content="{Binding Path=MyAppResources.OkSoeDialog, Source={StaticResource ResourcesWrapper}}" 
       Margin="5" > 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="Click"> 
         <Command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path= AddMessageTemplate.SaveCommand}" /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </telerik:RadButton> 
      <telerik:RadButton x:Name="btnCancel" Width="120" Content="{Binding Path=MyAppResources.CancelSoeDialog, Source={StaticResource ResourcesWrapper}}" 
       Margin="5" > 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="Click"> 
         <Command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path= AddMessageTemplate.CancelCommand}" /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 

      </telerik:RadButton> 
     </StackPanel> 
    </Grid> 
</telerik:RadWindow> 

뒤에 코드 radwindow

public partial class AddMessageTemplateView : RadWindow 
    { 
     /// <summary> 
     /// Initializes a new instance of the AddMessageTemplateView class. 
     /// </summary> 
     public AddMessageTemplateView() 
     { 
      InitializeComponent(); 

      (this.DataContext as AddMessageTemplateViewModel).RequestClose += Close; 
     } 
    } 



using System; 
using GalaSoft.MvvmLight.Command; 
using GalaSoft.MvvmLight.Messaging; 
using Telerik.Windows.Controls; 
using MyAppUtils.DataModel; 
using MyAppUtils.DataModel.Messaging; 
using ViewModelBase = GalaSoft.MvvmLight.ViewModelBase; 

namespace MyAppApp.ViewModel 
{ 
    /// <summary> 
    /// This class contains properties that a View can data bind to. 
    /// <para> 
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel. 
    /// </para> 
    /// <para> 
    /// You can also use Blend to data bind with the tool's support. 
    /// </para> 
    /// <para> 
    /// See http://www.galasoft.ch/mvvm/getstarted 
    /// </para> 
    /// </summary> 
    public class AddMessageTemplateViewModel : ViewModelBase 
    { 
     private MessageTemplate _newTemplate; 
     private MessageType _selectedtype; 
     private string SelectedTypePropertyName = "SelectedType"; 
     private string NewTemplatePropertyName = "NewTemplate"; 

     /// <summary> 
     /// Initializes a new instance of the AddMessageTemplateViewModel class. 
     /// </summary> 
     public AddMessageTemplateViewModel() 
     { 
      InitiateCommands(); 
      ////if (IsInDesignMode) 
      ////{ 
      //// // Code runs in Blend --> create design time data. 
      ////} 
      ////else 
      ////{ 
      //// // Code runs "for real": Connect to service, etc... 
      ////} 
     } 

     public MessageTemplate NewTemplate 
     { 
      get { return _newTemplate; } 


      set 
      { 
       if (_newTemplate != value) 
       { 
        _newTemplate = value; 
        RaisePropertyChanged(NewTemplatePropertyName); 
       } 
      } 
     } 

     public void InitiateCommands() 
     { 

      SaveCommand = new RelayCommand(Save); 
      CancelCommand = new RelayCommand(Cancel); 
     } 

     private void Cancel() 
     { 
      Messenger.Default.Send(new NotificationMessage(this, "Cancel")); 
      Close(); 
     } 

     private void Save() 
     { 
      //TODO: walidacja jak w customervehicle 
      if (String.IsNullOrEmpty(NewTemplate.Error)) 
      { 
       Messenger.Default.Send(new NotificationMessage<MessageTemplate>(this, NewTemplate, "Add")); 
       Close(); 
      } 
      else 
      { 
       RadWindow.Alert(NewTemplate.Error); 
      } 

     } 

     public RelayCommand SaveCommand { get; private set; } 
     public RelayCommand CancelCommand { get; private set; } 

     public MessageType SelectedType 
     { 
      get { return _selectedtype; } 
      set 
      { 
       if (_selectedtype != value) 
       { 
        _selectedtype = value; 
        RaisePropertyChanged(SelectedTypePropertyName); 
        if (_selectedtype.ToString() == MessageType.Email.ToString()) 
        { 
         NewTemplate = new EmailMessageTemplate(); 
        } 
        else if (_selectedtype.ToString() == MessageType.Sms.ToString()) 
        { 
         NewTemplate = new SmsMessageTemplate(); 
        } 
        else 
        { 
         NewTemplate = null; 
        } 
       } 
      } 
     } 

     public event Action RequestClose; 

     public virtual void Close() 
     { 
      if (RequestClose != null) 
      { 
       RequestClose(); 
      } 
     } 



    } 
} 

그리고 부모 컨트롤의 코드 부모 컨트롤의 viemodel의

private void HandleNotification(NotificationMessage obj) 
     { 
      if (obj.Notification == "ShowAddMessageTemplate") 
      { 
       var ShowAddMessageTemplateRadWindow = new AddMessageTemplateView(); 
       ShowAddMessageTemplateRadWindow.Show(); 
      } 
     } 

조각 조각 :

private void ShowAddMessageTemplate() 
     { 


      Messenger.Default.Send(new NotificationMessage(this, "ShowAddMessageTemplate")); 


      Messenger.Default.Register<NotificationMessage<MessageTemplate>>(this, HandleMessageTemplateAdding); 


     } 

     private void HandleMessageTemplateAdding(NotificationMessage<MessageTemplate> notificationMessage) 
     { 

      if (notificationMessage.Notification=="Add") 
      { 
       ServiceAgent.AddMessageTemplate(notificationMessage.Content,_App.HandlerId,GetMessageTemplates); 
      } 

     } 

답변

0

I 거래 AddMessageTemplateView의 생성자에 추가하여 그 문제 :

this.Closed += (s, e) => ViewModelLocator.ClearAddMessageTemplate(); 

ClearAddMessageTemplateI입니다 :

public static void ClearAddMessageTemplate() 
     { 
      _addMessageTemplate.Cleanup(); 
      _addMessageTemplate = null; 
     } 
관련 문제