2011-07-08 1 views
1

J'ai une fenêtre principale avec le code suivant:Liaison de données le titre de la fenêtre principale pour voir la propriété du modèle

<Window x:Class="CAMXSimulator.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:View="clr-namespace:CAMXSimulator.View" 
     xmlns:ViewModel="clr-namespace:CAMXSimulator.ViewModel" 
     Icon="Resources/Images/Tractor.png" 
     Title="{Binding WindowTitle}" 

     Height="400" Width="600"> 

    <Window.Resources> 
     <DataTemplate DataType="{x:Type ViewModel:LogParserViewModel}"> 
      <View:LogView /> 
     </DataTemplate> 
    </Window.Resources> 

     <Grid ShowGridLines="True"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 



     <Border CornerRadius="5" BorderBrush="SteelBlue" BorderThickness="2" Grid.Row="2" Margin="0,5,5,0" > 
      <View:LogView /> 
     </Border> 

    </Grid> 

</Window> 

dans la classe LogParserViewModel.cs j'ai les suivantes

EDIT:

class LogParserViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     // public event PropertyChangedEventHandler PropertyChanged1; 
     private IDbOperations _camxdb; 
     #region private_virables 
     private string _vManageLogFile; 
     private string _camxNodes; 
     private IEnumerable<Tuple<string, string>> _camxnodesAsTuple; 
     RelayCommand _clearFieldscommand; 
     RelayCommand _runsimulationcommand; 

     private string _currentProfileName; 

     #endregion 

     #region Getters\Setters 
     public string CurrentProfileName 
     { 
      get { return _currentProfileName; } 
      set 
      { 
       _currentProfileName = value; 
       OnPropertyChanged("CurrentProfileName"); 
       OnPropertyChanged("WindowTitle"); 
      } 
     } 



     public string VManageLogFile 
     { 
      get { return _vManageLogFile; } 
      set { _vManageLogFile = value; 

        if(null != PropertyChanged) 
        { 
         // PropertyChanged(this, new PropertyChangedEventArgs("VManageLogFile")); 
         OnPropertyChanged("VManageLogFile"); 
        } 
      } 
     } 

     public string CamxNodes 
     { 
      get { return _camxNodes; } 
      set 
      { 
       _camxNodes = value; 
       if (null != PropertyChanged) 
       { 
        //PropertyChanged1(this, new PropertyChangedEventArgs("CamxNodes")); 
        OnPropertyChanged("CamxNodes"); 
       } 

      } 
     } 
     #endregion 

     protected void OnPropertyChanged(string name) 
     { 
      // PropertyChangedEventHandler handler = PropertyChanged; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(name)); 
      } 
     } 

     #region Constructors 
     public LogParserViewModel() 
     { 
      // PropertyChanged1 = new PropertyChangedEventHandler(); 
      //PropertyChanged += UpdateCamxWindowEvent; 
      PropertyChanged += (s, e) => { if (e.PropertyName == "VManageLogFile") UpdateCamxWindowEvent(s, e); }; 

      //creates a instance of database object 
      _camxdb = new DbOperations(); 


     } 
     #endregion 

     #region Event_Hendlers 
     /// <summary> 
     /// This event is called when vManageLog window has changed 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void UpdateCamxWindowEvent(object sender, EventArgs e) 
     { 
      if (_vManageLogFile == null) 
       return; 

      //creates object of parser 
      var parser = new VManageLogParser(_vManageLogFile); 
      //returns a tuple of string string 
      _camxnodesAsTuple = parser.Parse(); 
      //creates a string as we see it in the CAMX window of the simulator 
      CamxNodes = parser.CamxWindowText2(_camxnodesAsTuple); 
      MyLogger.Logger.Info("The Tabs been updated"); 

      CurrentProfileName = "CAMX Simulator"; 


     } 
     #endregion 

     #region Drag & DragOver 
     public void DragOver(DragEventArgs args) 
     { 
      // As an arbitrary design decision, we only want to deal with a single file. 
      if (IsSingleTextFile(args) != null) args.Effects = DragDropEffects.Copy; 
      else args.Effects = DragDropEffects.None; 

      // Mark the event as handled, so TextBox's native DragOver handler is not called. 
      args.Handled = true; 
     } 

     public void Drop(DragEventArgs args) 
     { 
      using (new WaitCursor()) 
      { 


       // Mark the event as handled, so TextBox's native Drop handler is not called. 
       args.Handled = true; 

       string fileName = IsSingleTextFile(args); 
       if (fileName == null) return; 

       StreamReader fileToLoad = new StreamReader(fileName); 
       VManageLogFile = fileToLoad.ReadToEnd(); 
       // DisplaySFMFileContents.Text = fileToLoad.ReadToEnd(); 

       fileToLoad.Close(); 

      } 
     } 

     // If the data object in args is a single file, this method will return the filename. 
     // Otherwise, it returns null. 
     private string IsSingleTextFile(DragEventArgs args) 
     { 
      // Check for files in the hovering data object. 
      if (args.Data.GetDataPresent(DataFormats.FileDrop, true)) 
      { 
       string[] fileNames = args.Data.GetData(DataFormats.FileDrop, true) as string[]; 
       // Check fo a single file or folder. 
       if (fileNames.Length == 1) 
       { 
        // Check for a file (a directory will return false). 
        if (File.Exists(fileNames[0])) 
        { 
         //Check for the file extention , we look only for txt extentions 
         FileInfo info = new FileInfo(fileNames[0]); 
         if (info.Extension == ".txt") 
         { 
          MyLogger.Logger.Info("Name of file: " + fileNames[0]); 
          // At this point we know there is a single file text file.); 
          return fileNames[0]; 
         } 

        } 
       } 
      } 
      MyLogger.Logger.Warn("Not a single file"); 
      return null; 
     } 
     #endregion 

     #region ClearCommand 

     public ICommand ClearFieldsCommand 
     { 
      get 
      { 
       if (_clearFieldscommand == null) 
        _clearFieldscommand = new RelayCommand(
         () => ClearFields(), 
         () => CanClearWindows); 

       return _clearFieldscommand; 
      } 
     } 

     void ClearFields() 
     { 
      VManageLogFile = null; 
      CamxNodes = null; 
     } 
     bool CanClearWindows 
     { 
      get { return (VManageLogFile != null); } 
     } 


     #endregion 

     #region RunSimulation 
     public ICommand RunSimulationCommand 
     { 
      get 
      { 
       if (_runsimulationcommand == null) 
        _runsimulationcommand = new RelayCommand(
         () => RunSimulation(), 
         () => CanRunSimulation); 

       return _runsimulationcommand; 
      } 
     } 

     void RunSimulation() 
     { 
      using (new WaitCursor()) 
      { 
       try 
       { //inserting the CAMX nodes to the table 
        foreach (var camxNode in _camxnodesAsTuple) 
        { 
         _camxdb.Insert(camxNode); 

        } 
       } 
       catch (Exception ex) 
       { 

        MyLogger.Logger.FatalException("Cannot Insert to Database" , ex); 
       } 

      } 
     } 

     bool CanRunSimulation 
     { 
      get { return !GlobalMethods.IsEmpty(_camxnodesAsTuple); } 
     } 
     #endregion 
    } 
} 

Et j'essaie de changer le titre des fenêtres en le saisissant mais rien ne se passe aucune idée pourquoi?

+1

Vous avez besoin de plus de code et XAML pour nous laisser vous aider. A savoir, à quoi est défini le DataContext de votre fenêtre principale? –

+0

Aussi, utilisez-vous des frameworks (Prism, Caliburn, etc)? –

+0

@ myermian, j'ai ajouté tout le code, et je n'utilise pas de framework –

Répondre

1

Comme je ne vois pas du code actuel que le DataContext du main.xaml est, je vais prendre une conjecture que c'est lui-même (pas réglé sur autre chose). Je vais aller plus loin dire que votre intention est de mettre DataContext à un ViewModel du main.xaml:

XAML:

<Window x:Class="Namespace.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="{Binding WindowTitle}"> 

    <!-- YOUR XAML --> 

</Window> 

code Derrière:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new MainWindowViewModel(); 
    } 
} 

Où MainWindowViewModel. cs contient la propriété pour le WindowTitle. Si vous voulez qu'une autre classe contrôle le WindowTitle, vous devez toujours avoir un ViewModel pour votre MainWindow (ie MainWindowViewModel.cs) qui accepte les messages d'une façon ou d'une autre (événements pour couplage serré, agrégation d'événements pour couplage lâche) mettre à jour cette propriété.

0

Votre propriété en ViewModel devrait être nommé WindowTitle au lieu de CurrentProfileName

+1

ou changez la liaison dans xaml pour Title = "{Binding CurrentProfileName}" et dans le code OnPropertyChanged ("CurrentProfileName"); – Nagg

+0

Je l'ai essayé mais cela n'a pas fonctionné –

+0

Bien qu'il soit possible que ce soit le problème, l'OP pourrait également augmenter l'événement changé de propriété 'WindowTitle' car il y a une autre propriété nommée' WindowTitle' qui est affectée par 'CurrentProfileName' changement de propriété. –

Questions connexes