2017-06-16 9 views
1

Très nouveau pour le codage WPF utilisant MVVM. J'ai essayé de faire une simple calculatrice dans WPF en utilisant MVVM. Mais incapable de déclencher l'Icommand dans le code ci-dessous.Si possible, aidez-moi dans ce domaine. Reconnaissant si quelqu'un peut m'aider.
Afficher le code:Impossible d'utiliser MVVM/liaison correctement dans le code ci-dessous et ayant des problèmes avec la propriété Icommand

<Window x:Class="MVVMCalculator.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:MVVMCalculator" 
     mc:Ignorable="d" 
     Title="Calculator" Height="350" Width="300"> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="85"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <TextBox Text="{Binding Display, Mode=OneWay}" IsReadOnly="True" TextWrapping="Wrap" 
       Grid.Row="0" Background="#E2E2E2" Margin="0,10,0,0" VerticalAlignment="Top" 
       Height="75" Width="250" HorizontalAlignment="Center" FontSize="22" FontWeight="Bold" 
       TextAlignment="Right"> 
      <TextBox.Effect> 
       <DropShadowEffect/> 
      </TextBox.Effect> 
     </TextBox> 
     <ItemsControl Grid.Row="1" ItemsSource="{Binding Buttns}" Margin="15,15,15,10"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Columns="5" Rows="4" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Content="{Binding Txt, Mode=TwoWay}" Command="{Binding Enter_number}" 

          FontSize="18" FontWeight="Bold" Height="50" Width="50" Background="#eef2f3" 
          BorderBrush="Black" BorderThickness="1.0" Name="number"> 
         <Button.Effect> 
          <DropShadowEffect/> 
         </Button.Effect> 
        </Button> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 
</Window> 

code ViewModel:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Input; 

namespace MVVMCalculator 
{ 
    class ViewModel : INotifyPropertyChanged 
    { 
     Buttons btn = new Buttons(); 
     private decimal operand1; 
     private decimal operand2; 
     private string operation; 
     private decimal result; 
     private string display; 
     private bool newDisplayRequired = false; 
     ObservableCollection<Buttons> buttns; 
     public ObservableCollection<Buttons> Buttns 
     { 
      get { return buttns; } 
      set { buttns = value; } 
     } 
     public decimal Result 
     { 
      get { return result; } 
     } 
     public decimal Operand1 
     { 
      get { return operand1; } 
      set { operand1 = value; } 
     } 
     public decimal Operand2 
     { 
      get { return operand2; } 
      set { operand2 = value; } 
     } 
     public string Operation 
     { 
      get { return operation; } 
      set { operation = value; } 
     } 
     public string Display 
     { 
      get { return display; } 
      set { display = value; 
       OnPropertyChanged("Display"); 
      } 
     } 

     public ViewModel() 
     { 
      buttns = new ObservableCollection<Buttons> 
      { 
       new Buttons("1"), new Buttons("2"), new Buttons("3"), 
       new Buttons("C"), new Buttons("Back"), new Buttons("4"), 
       new Buttons("5"), new Buttons("6"), new Buttons("CE"), 
       new Buttons("%"), new Buttons("7"), new Buttons("8"), 
       new Buttons("9"), new Buttons("/"), new Buttons("*"), 
       new Buttons("0"), new Buttons("."), new Buttons("+"), 
       new Buttons("-"), new Buttons("=") 
      }; 
      display = "0"; 
      operand1 = 0; 
      operand2 = 0; 
      operation = ""; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

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

     private ICommand enter_number; 
     public ICommand Enter_number 
     { 
      get 
      { 
       if(enter_number==null) 
       { 
        enter_number = new DelegateCommand<string>(MyAction, _canExecute); 
       } 
       return enter_number; 
      } 
     } 
     private static bool _canExecute(string button) 
     { 
      return true; 
     } 
     public void MyAction(string btn) 
     { 
      switch(btn) 
      { 
       case "C": 
        display = "0"; 
        operand1 = 0; 
        operand2 = 0; 
        //operation = ""; 
        break; 
       case ".": 
        if (!display.Contains(".")) 
        { 
         Display = display + "."; 
        } 
        break; 
       case "Back": 
        if (display.Length > 1) 
         Display = display.Substring(0, display.Length - 1); 
        else Display = "0"; 
        break; 
       default: 
        if (display == "0" || newDisplayRequired) 
         Display = btn; 
        else 
         Display = display + btn; 
        break; 
      } 
     } 
    } 
} 

Boutons Classe:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace MVVMCalculator 
{ 
    class Buttons:INotifyPropertyChanged 
    { 
     private string txt; 
     public string Txt 
     { 
      get { return txt; } 
      set { txt = value; } 
     } 

     public Buttons(string a) 
     { 
      txt = a; 
     } 

     public Buttons() 
     { 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

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

Xaml.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace MVVMCalculator 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new ViewModel(); 
     } 
    } 
} 
+0

Veuillez décrire votre problème plus en détail. –

+0

Quand je cours, je peux voir l'interface utilisateur avec des boutons dessus et je peux déboguer le programme jusque là, mais quand je clique sur n'importe quel bouton, je ne peux pas déboguer le programme et aucun changement que je peux voir. Probablement, il y a un problème avec Icommand! – Renee

+0

Vous définissez 'display' privé lorsque vous devez définir' Display' public, c'est ce qui va déclencher la modification de l'interface utilisateur. Mais cela ne s'applique que lorsque la chaîne est 'C'. – XAMlMAX

Répondre

0

Depuis le Enter_number propriété est définie dans la classe ViewModel vous devez utiliser un {RelativeSource} pour être capable de se lier à elle:

<Button Content="{Binding Txt, Mode=TwoWay}" 
      Command="{Binding DataContext.Enter_number, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
      FontSize="18" FontWeight="Bold" Height="50" Width="50" Background="#eef2f3" 
      BorderBrush="Black" BorderThickness="1.0" Name="number"> 
     <Button.Effect> 
      <DropShadowEffect/> 
     </Button.Effect> 
    </Button> 

La valeur par défaut DataContext du Button est l'objet Buttons courant dans la collection ItemsSource du ItemsControl et c'est pourquoi votre liaison échoue.

+0

merci beaucoup pour votre temps et votre suggestion. Je l'ai marqué comme une réponse. Pouvez-vous s'il vous plaît me diriger vers le lien où je peux apprendre la liaison des données de l'interface utilisateur à la source. – Renee

+0

Veuillez vous référer à l'article suivant: http://www.c-sharpcorner.com/UploadFile/yougerthen/relativesources-in-wpf/ – mm8