2016-12-01 22 views
0

我是WPF/MVVM的初學者,我有很多問題.. 其中之一,它是我的應用程序在左邊有一個LoginView,在內容視圖在剩下。 論文2次是在的MainView這樣的: 首先查看 「主窗口」已啓用瀏覽我的應用程序後查看

<Window x:Class="MainWindow" 
    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:local="clr-namespace:iut1" 
    xmlns:ctrls="clr-namespace:iut1.Views" 
    mc:Ignorable="d" 
    Title="Outil de pilotage SCR" Height="650" Width="950" 
    WindowStartupLocation="CenterScreen" 
    DataContext="{Binding Path=MainWindowViewModel, Mode=OneWay, Source={StaticResource Locator}}"> 
<Grid> 
    <Viewbox Name="vbxConnexion" HorizontalAlignment="Left" Height="623" VerticalAlignment="Top" Width="224"> 
     <ctrls:ConnexionView></ctrls:ConnexionView> 
    </Viewbox> 
    <Viewbox Name="vbxContenu" HorizontalAlignment="Left" Margin="233,0,0,0" Width="682" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Height="620" Stretch="None"> 
     <ctrls:MigrationModeleView IsEnabled="{Binding EnabledView}" Height="623" Width="653" ></ctrls:MigrationModeleView> 
    </Viewbox> 


</Grid> 

Seconde系列查看: 「ConnexionView」

<UserControl x:Class="iut1.Views.ConnexionView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:iut1.Views" 
     xmlns:w="clr-namespace:iut1.Classes" 
     mc:Ignorable="d" 
     d:DesignHeight="720" d:DesignWidth="240" 
      DataContext="{Binding Path=MainWindowViewModel, Mode=OneWay, Source={StaticResource Locator}}"> 
<UserControl.Resources> 
    <w:EnumMatchToBooleanConverter x:Key="enumConverter" /> 
</UserControl.Resources> 
<Grid Margin="10,30,-13,-18"> 

    <GroupBox x:Name="gpbSource" HorizontalAlignment="Left" Margin="6,10,0,386" Width="220" Header="Environnement source" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" > 
     <Grid Margin="0,0,0,20"> 
      <!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,5,0,0"/>--> 
      <Label x:Name="lblIdentifiant" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/> 

      <TextBox x:Name="txtNomUsager" Text="{Binding IdUtilisateur}" HorizontalAlignment="Left" Height="23" Margin="4,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AutomationProperties.IsRequiredForForm="True" BorderThickness="2"> 
      </TextBox> 

      <Label x:Name="Password" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/> 
      <PasswordBox x:Name="txtMotDePasse" w:PasswordHelper.Attach="True" w:PasswordHelper.Password="{Binding PasswordUtilisateur, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/> 
      <RadioButton x:Name="rdbUnit" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Unitaire}" GroupName="envSource" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbFonctionnel" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Fonctionnel}" Content="Fonctionnel" GroupName="envSource" Width="79" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbIntegre" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Integre}" GroupName="envSource" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbAcceptation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Acceptation}" GroupName="envSource" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbFormation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Formation}" GroupName="envSource" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbProduction" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay,Converter={StaticResource enumConverter}, ConverterParameter=Production}" GroupName="envSource" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/> 
      <Button x:Name="Login" Content="Connexion" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200" Command="{Binding ButtonCommand}"/> 
     </Grid> 
    </GroupBox> 

    <!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,350,0,-179"/>--> 
    <GroupBox Width="220" Header="Environnement source" Margin="6,338,17,57" IsEnabled="False" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}"> 
     <Grid Margin="0,0,0,20"> 
      <Label x:Name="lblIdentifiantCopie" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/> 
      <TextBox x:Name="txtNomUsagerCopie" HorizontalAlignment="Left" Height="23" Margin="4,47,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/> 
      <Label x:Name="lblMotDePasseCopie" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/> 
      <PasswordBox x:Name="txtMotDePasseCopie" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/> 
      <RadioButton x:Name="rdbUnitCopie" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbFonctionnelCopie" Width="79" Content="Fonctionnel" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbIntegreCopie" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbAcceptationCopie" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbFormationCopie" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/> 
      <RadioButton x:Name="rdbProductionCopie" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/> 
      <Button x:Name="btnAjout" Content="Ajouter dans l'environnement" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200"/> 
     </Grid> 
    </GroupBox> 


</Grid> 

MigrationModelView

<UserControl x:Class="iut1.Views.MigrationModeleView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:iut1.Views" 
     mc:Ignorable="d" d:DesignWidth="630" Height="587" 
     xmlns:w="clr-namespace:iut1.Classes" 
     DataContext="{Binding Path=MainWindowViewModel, Source={StaticResource Locator}}"> 

<Grid Margin="15,22,0,0"> 
    <GroupBox Header="Migration de modèle de lettre" Margin="6,10,0,32"> 
     <Grid Margin="0,0,0,4"> 
      <Label x:Name="lblChoixPPP" Content="PPP :" HorizontalAlignment="Left" Margin="19,13,0,0" VerticalAlignment="Top"/> 
      <ComboBox x:Name="cbxChoixPPP" HorizontalAlignment="Left" Margin="67,13,0,0" VerticalAlignment="Top" Width="120"/> 
      <DataGrid x:Name="dgModeles" HorizontalAlignment="Left" Margin="19,61,0,0" VerticalAlignment="Top" Height="162" Width="565"> 
       <DataGrid.Columns> 
        <DataGridCheckBoxColumn Header=""/> 
        <DataGridTextColumn Header="Numéro de référence" Width="150" /> 
        <!--Binding="{Binding Nom}"--> 
        <DataGridTextColumn Header="Titre du modèle de lettre" /> 
        <!--Binding="{Binding Prenom}"--> 
       </DataGrid.Columns> 
      </DataGrid> 
      <Label x:Name="lblRechercher" Content="Rechercher :" HorizontalAlignment="Left" Margin="19,239,0,0" VerticalAlignment="Top"/> 
      <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="91,241,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="291"/> 
      <Button x:Name="btnAfficherTout" Content="Afficher tout" HorizontalAlignment="Left" Margin="387,244,0,0" VerticalAlignment="Top" Width="96"/> 
      <Button x:Name="btnAfficherSelection" IsEnabled="False" Content="Afficher sélection" HorizontalAlignment="Left" Margin="488,244,0,0" VerticalAlignment="Top" Width="96"/> 
      <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="19,319,0,0" VerticalAlignment="Top" Height="162" Width="565"> 
       <DataGrid.Columns> 
        <DataGridTextColumn Header="Résultat de la copie" Width="150" /> 
        <DataGridTextColumn Header="" /> 
       </DataGrid.Columns> 
      </DataGrid> 
      <ProgressBar HorizontalAlignment="Left" Height="20" Margin="22,509,0,-26" VerticalAlignment="Top" Width="464"/> 
      <Button x:Name="btnDetails" Height="20" IsEnabled="False" Content="Détails" HorizontalAlignment="Left" Margin="491,509,0,-26" VerticalAlignment="Top" Width="96"/> 
     </Grid> 
    </GroupBox> 
</Grid> 

我想啓用=真點擊我的ConnexionView登錄按鈕後,我的看法。 我嘗試了很多東西,沒有任何工作。

MY VM代碼:

using GalaSoft.MvvmLight.CommandWpf; 
using iut1.Classes; 
using iut1.Interfaces; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Security; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Input; 

namespace iut1.ViewModels 
{ 
/// <summary> 
/// 
/// </summary> 
public class MainWindowViewModel : BaseViewModel, INotifyPropertyChanged 
{ 
    #region Proprietes privées 
    private readonly DelegateCommand<string> _clickCommand; 



    private string _idUtilisateur; 
    private string _passwordUtilisateur; 
    private string _environnementSource; 
    #endregion Proprietes privées 

    #region proprietes publiques 
    public string IdUtilisateur 
    { 
     get { return _idUtilisateur; } 
     set 
     { 
      _idUtilisateur = value; 
      RaisePropertyChanged(nameof(IdUtilisateur)); 
     } 
    } 
    public string PasswordUtilisateur 
    { 
     get { return _passwordUtilisateur; } 
     set 
     { 
      _passwordUtilisateur = value; 
      RaisePropertyChanged(nameof(PasswordUtilisateur)); 
     } 
    } 
    public string EnvironnementSource 
    { 
     get { return _environnementSource; } 
     set 
     { 
      _environnementSource = value; 
      RaisePropertyChanged(nameof(EnvironnementSource)); 
     } 
    } 
    #endregion proprietes publiques 

    private bool _enabledView; 
    public bool EnabledView 
    { 
     get { return _enabledView; } 
     set 
     { 
      if (_enabledView == value) 
      { 
       return; 
      } 

       _enabledView = value; 
      RaisePropertyChanged(nameof(EnabledView)); 
      OnPropertyChanged(nameof(EnabledView)); 
     } 
    } 


    public MainWindowViewModel() 
    { 


     //ButtonCommand = new Classes.RelayCommand(new Action<object> (ConnectionBase)); 
     ButtonCommand = new Classes.RelayCommand(o => { ConnectionBase(); }, o => true); 


     //_clickCommand = new DelegateCommand<string>(
     // (s) => { ConnectionBase(); /* perform some action */ } 
     // ); 

     EnabledView =false; 
    } 


    public DelegateCommand<string> ButtonClickCommand 
    { 
     get { return _clickCommand; } 
    } 



    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 


    private void ConnectionBase() 
    { 
     if (Validation.ValidationConnection(IdUtilisateur, PasswordUtilisateur, EnvironnementSource)) 
     { 
      EnabledView = true; 
      OnPropertyChanged(nameof(EnabledView)); 

     } 
     else 
     { 
      string msgErreur = "KO"; 
     } 
    } 



    private void Login(object parameter) 
    { 


     //return Validation.ValidationConnection(IdUtilisateur,  password, "ENV"); 

    } 


    private ICommand _buttonCommand; 
    public ICommand ButtonCommand 
    { 
     get 
     { 
      return _buttonCommand; 
     } 
     set 
     { 
      _buttonCommand = value; 
     } 
    } 


} 
} 

的問題是,我的觀點nerver成爲能和我成爲瘋狂有關。

非常感謝您的幫助。

+0

1.你能證明你的'RaisePropertyChanged'方法的內容。 2。你可以證實,你的MainWindowViewModel類實現了INotifyPropertyChanged接口嗎? 3.你能確認一下,'Grid'的父母有一個'MainWindowViewModel'的實例設置爲'DataContext'嗎? –

+0

我用我的虛擬機的完整代碼更新我的帖子 – Jaymz1982

+0

首先,您在不同的地方多次使用'OnPropertyChanged',請參閱我對下面答案的評論,並讓我們清理您的代碼。 – ColinM

回答

0

重要的是,您要提高INotifyPropertyChanged事件一次,並在正確的位置,以便所有'訂閱者'都知道舊值和新值的變化。由於您多次舉辦活動,我懷疑「訂閱者」基本上被告知價值已更新 - 舊值:false,新值:false。

沒有看到您的解決方案和最新的代碼的全部,這是很難準確地診斷問題,這裏是我剛纔從你在你原來的職位是什麼重構的代碼,點擊按鈕調用ButtonCommand能立刻將EnabledView屬性與它當前的屬性相反,這爲我啓用和禁用了MigrationModeleView。

的App.xaml

<Application x:Class="MvvmLight1.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:vm="clr-namespace:MvvmLight1.ViewModel" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:ignore="http://www.galasoft.ch/ignore" 
      StartupUri="MainWindow.xaml" 
      mc:Ignorable="d ignore"> 
    <Application.Resources> 
     <!--Global View Model Locator--> 
     <vm:ViewModelLocator x:Key="Locator" 
          d:IsDataSource="True" /> 
    </Application.Resources> 
</Application> 

主窗口

<Window x:Class="MvvmLight1.MainWindow" 
     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:ignore="http://www.galasoft.ch/ignore" 
     xmlns:mvvmLight1="clr-namespace:MvvmLight1" 
     mc:Ignorable="d ignore" 
     Height="650" 
     Width="950" 
     Title="MVVM Light Application" 
     DataContext="{Binding Main, Source={StaticResource Locator}}"> 

    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Skins/MainSkin.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources> 

    <Grid> 
     <Viewbox Name="vbxConnexion" HorizontalAlignment="Left" Height="623" VerticalAlignment="Top" Width="224"> 
      <mvvmLight1:ConnexionView></mvvmLight1:ConnexionView> 
     </Viewbox> 
     <Viewbox Name="vbxContenu" HorizontalAlignment="Left" Margin="233,0,0,0" Width="682" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Height="620" Stretch="None"> 
      <mvvmLight1:MigrationModeleView IsEnabled="{Binding EnabledView}" Height="623" Width="653" ></mvvmLight1:MigrationModeleView> 
     </Viewbox> 
    </Grid> 
</Window> 

ConnexionView.xaml

<UserControl x:Class="MvvmLight1.ConnexionView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:MvvmLight1" 
      mc:Ignorable="d" 
      d:DesignHeight="720" d:DesignWidth="240" 
      DataContext="{Binding Main, Source={StaticResource Locator}}"> 
    <Grid Margin="10,30,-13,-18"> 

     <GroupBox x:Name="gpbSource" HorizontalAlignment="Left" Margin="6,10,0,386" Width="220" Header="Environnement source" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}" > 
      <Grid Margin="0,0,0,20"> 
       <!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,5,0,0"/>--> 
       <Label x:Name="lblIdentifiant" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/> 

       <TextBox x:Name="txtNomUsager" Text="{Binding IdUtilisateur}" HorizontalAlignment="Left" Height="23" Margin="4,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AutomationProperties.IsRequiredForForm="True" BorderThickness="2"> 
       </TextBox> 

       <Label x:Name="Password" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/> 
       <PasswordBox x:Name="txtMotDePasse" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/> 
       <RadioButton x:Name="rdbUnit" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Unitaire}" GroupName="envSource" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbFonctionnel" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Fonctionnel}" Content="Fonctionnel" GroupName="envSource" Width="79" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbIntegre" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Integre}" GroupName="envSource" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbAcceptation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Acceptation}" GroupName="envSource" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbFormation" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Formation}" GroupName="envSource" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbProduction" IsChecked="{Binding Path=EnvironnementSource, Mode=TwoWay, ConverterParameter=Production}" GroupName="envSource" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/> 
       <Button x:Name="Login" Content="Connexion" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200" Command="{Binding ButtonCommand}"/> 
      </Grid> 
     </GroupBox> 

     <!--<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="317" VerticalAlignment="Top" Width="220" RadiusY="5" RadiusX="5" Stroke="Black" Opacity="0.1" Margin="0,350,0,-179"/>--> 
     <GroupBox Width="220" Header="Environnement source" Margin="6,338,17,57" IsEnabled="False" Background="{DynamicResource {x:Static SystemColors.ScrollBarBrushKey}}"> 
      <Grid Margin="0,0,0,20"> 
       <Label x:Name="lblIdentifiantCopie" Content="Identifiant de l'utilisateur :" HorizontalAlignment="Left" Margin="4,24,0,0" VerticalAlignment="Top" Width="200"/> 
       <TextBox x:Name="txtNomUsagerCopie" HorizontalAlignment="Left" Height="23" Margin="4,47,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/> 
       <Label x:Name="lblMotDePasseCopie" Content="Mot de passe :" HorizontalAlignment="Left" Margin="4,80,0,0" VerticalAlignment="Top" Width="200"/> 
       <PasswordBox x:Name="txtMotDePasseCopie" HorizontalAlignment="Left" Height="23" Margin="4,103,0,0" VerticalAlignment="Top" Width="200"/> 
       <RadioButton x:Name="rdbUnitCopie" Width="79" Content="Unitaire" HorizontalAlignment="Left" Margin="4,160,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbFonctionnelCopie" Width="79" Content="Fonctionnel" HorizontalAlignment="Left" Margin="4,183,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbIntegreCopie" Width="79" Content="Intégré" HorizontalAlignment="Left" Margin="4,206,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbAcceptationCopie" Width="90" Content="Acceptation" HorizontalAlignment="Left" Margin="114,162,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbFormationCopie" Width="90" Content="Formation" HorizontalAlignment="Left" Margin="114,183,0,0" VerticalAlignment="Top"/> 
       <RadioButton x:Name="rdbProductionCopie" Width="90" Content="Production" HorizontalAlignment="Left" Margin="114,206,0,0" VerticalAlignment="Top"/> 
       <Button x:Name="btnAjout" Content="Ajouter dans l'environnement" HorizontalAlignment="Left" Margin="4,252,0,0" VerticalAlignment="Top" Width="200"/> 
      </Grid> 
     </GroupBox> 
    </Grid> 
</UserControl> 

MigrationModeleView.xaml

<UserControl x:Class="MvvmLight1.MigrationModeleView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:MvvmLight1" 
      mc:Ignorable="d" d:DesignWidth="630" Height="587" 
      DataContext="{Binding Main, Source={StaticResource Locator}}"> 
    <Grid Margin="15,22,0,0"> 
     <GroupBox Header="Migration de modèle de lettre" Margin="6,10,0,32"> 
      <Grid Margin="0,0,0,4"> 
       <Label x:Name="lblChoixPPP" Content="PPP :" HorizontalAlignment="Left" Margin="19,13,0,0" VerticalAlignment="Top"/> 
       <ComboBox x:Name="cbxChoixPPP" HorizontalAlignment="Left" Margin="67,13,0,0" VerticalAlignment="Top" Width="120"/> 
       <DataGrid x:Name="dgModeles" HorizontalAlignment="Left" Margin="19,61,0,0" VerticalAlignment="Top" Height="162" Width="565"> 
        <DataGrid.Columns> 
         <DataGridCheckBoxColumn Header=""/> 
         <DataGridTextColumn Header="Numéro de référence" Width="150" /> 
         <!--Binding="{Binding Nom}"--> 
         <DataGridTextColumn Header="Titre du modèle de lettre" /> 
         <!--Binding="{Binding Prenom}"--> 
        </DataGrid.Columns> 
       </DataGrid> 
       <Label x:Name="lblRechercher" Content="Rechercher :" HorizontalAlignment="Left" Margin="19,239,0,0" VerticalAlignment="Top"/> 
       <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="91,241,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="291"/> 
       <Button x:Name="btnAfficherTout" Content="Afficher tout" HorizontalAlignment="Left" Margin="387,244,0,0" VerticalAlignment="Top" Width="96"/> 
       <Button x:Name="btnAfficherSelection" IsEnabled="False" Content="Afficher sélection" HorizontalAlignment="Left" Margin="488,244,0,0" VerticalAlignment="Top" Width="96"/> 
       <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="19,319,0,0" VerticalAlignment="Top" Height="162" Width="565"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="Résultat de la copie" Width="150" /> 
         <DataGridTextColumn Header="" /> 
        </DataGrid.Columns> 
       </DataGrid> 
       <ProgressBar HorizontalAlignment="Left" Height="20" Margin="22,509,0,-26" VerticalAlignment="Top" Width="464"/> 
       <Button x:Name="btnDetails" Height="20" IsEnabled="False" Content="Détails" HorizontalAlignment="Left" Margin="491,509,0,-26" VerticalAlignment="Top" Width="96"/> 
      </Grid> 
     </GroupBox> 
    </Grid> 
</UserControl> 

MainViewModel.cs

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 

namespace MvvmLight1.ViewModel 
{ 
    /// <summary> 
    /// This class contains properties that the main View can data bind to. 
    /// <para> 
    /// See http://www.mvvmlight.net 
    /// </para> 
    /// </summary> 
    public class MainViewModel : ViewModelBase 
    { 
     public MainViewModel() 
     { 
     } 

     public RelayCommand ButtonCommand 
     { 
      get { return new RelayCommand(() => { EnabledView = !EnabledView; }); } 
     } 

     private bool _environmentSource; 

     public bool EnvironnementSource 
     { 
      get { return _environmentSource; } 
      set { Set(ref _environmentSource, value); } 
     } 

     private string _idUtilisateur; 
     public string IdUtilisateur 
     { 
      get { return _idUtilisateur; } 
      set { Set(ref _idUtilisateur, value); } 
     } 

     private bool _enabledView; 
     public bool EnabledView 
     { 
      get { return _enabledView; } 
      set { Set(ref _enabledView, value); } 
     } 
    } 
} 

ViewModelLocator.cs

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Ioc; 
using Microsoft.Practices.ServiceLocation; 

namespace MvvmLight1.ViewModel 
{ 
    /// <summary> 
    /// This class contains static references to all the view models in the 
    /// application and provides an entry point for the bindings. 
    /// <para> 
    /// See http://www.mvvmlight.net 
    /// </para> 
    /// </summary> 
    public class ViewModelLocator 
    { 
     static ViewModelLocator() 
     { 
      ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 
      SimpleIoc.Default.Register<MainViewModel>(); 
     } 

     /// <summary> 
     /// Gets the Main property. 
     /// </summary> 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", 
      "CA1822:MarkMembersAsStatic", 
      Justification = "This non-static member is needed for data binding purposes.")] 
     public MainViewModel Main 
     { 
      get 
      { 
       return ServiceLocator.Current.GetInstance<MainViewModel>(); 
      } 
     } 

     /// <summary> 
     /// Cleans up all the resources. 
     /// </summary> 
     public static void Cleanup() 
     { 
     } 
    } 
} 
+0

非常感謝。 我試着讓我的代碼看起來像你的,但沒有任何變化。 它可能不起作用,因爲我的登錄按鈕是在視圖中,我的用戶控件是在另一個視圖中? 這兩個具有相同的虛擬機? – Jaymz1982

+0

請檢查我的更新後的帖子。你能否用兩個視圖的XAML代碼更新原始文章? – ColinM

+0

我把你的代碼放在我的地方,但它仍然不起作用... 你有沒有試過在2個不同的視圖中這樣做,像我一樣? – Jaymz1982

相關問題