2016-05-18 7 views
0

だから私は厄介な問題に直面しています。WPF双方向モードのバインディングは、INotifyPropertyChangedを使用していません。

namespace Market.ViewModel 
{ 
    public class BillingViewModel : ViewModelBase 
    { 
     #region Private Members 

     private Customer _customer; 
     private Product _products; 
     private string _productId = "asd"; 
     RelayCommand _numClickedCommand; 

     #endregion 

     public Customer Customer 
     { 
      get { return _customer; } 
      set 
      { 
       _customer = value; 
       NotifyPropertyChanged("Customer"); 
      } 
     } 

     public Product Products 
     { 
      get { return _products; } 
      set 
      { 
       _products = value; 
       NotifyPropertyChanged("Products"); 
      } 
     } 

     public bool CanClick 
     { 
      get { return true; } 
     } 

     public string ProductId 
     { 
      get { return _productId; } 
      set 
      { 
       _productId = value; 
       NotifyPropertyChanged("ProductId"); 
      } 
     } 

     public ICommand NumClickedCommand 
     { 
      get 
      { 
       if (_numClickedCommand == null) 
       { 
        _numClickedCommand = new RelayCommand(param => this.NumClicked(param.ToString()), 
         param => this.CanClick); 
       } 
       return _numClickedCommand; 
      } 
     } 

     #region PrivateMethods 

     private void NumClicked(string numClicked) 
     { 
      ProductId = ProductId+numClicked; 
     } 

     #endregion 
    } 
} 

それが継承ViewModelBase INotifyPropertyCangedを実装します。これが私の見解モデルです。

public class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

そして、私の見解は以下のとおりです。NumClickedCommandこのXAMLで使用されている

<Window x:Class="Billing.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:controls="clr-namespace:Billing" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:viewModel="clr-namespace:Market.ViewModel;assembly=Market.ViewModel" 
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" 
    Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <viewModel:BillingViewModel x:Key="ViewModel" /> 
    </Window.Resources> 
    <Grid DataContext="{Binding Source={StaticResource ViewModel}}"> 
     <controls:NumPad HorizontalAlignment="Left" Margin="265,205,0,-192" VerticalAlignment="Top" Height="307" Width="242"/> 
     <TextBox HorizontalAlignment="Left" Height="23" Margin="247,29,0,0" TextWrapping="Wrap" Text="{Binding ProductId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/> 
    </Grid> 
</Window> 

<UserControl x:Class="Billing.NumPad" 
     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:viewModel="clr-namespace:Market.ViewModel;assembly=Market.ViewModel" 
     mc:Ignorable="d" Height="109" Width="248"> 

    <UserControl.Resources> 
     <viewModel:BillingViewModel x:Key="ViewModel"/> 
    </UserControl.Resources> 
    <Grid Height="109" VerticalAlignment="Top" DataContext="{Binding Source={StaticResource ViewModel}}"> 
     <Button Content="1" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="75" CommandParameter="1" Command="{Binding NumClickedCommand}"/> 
     <Button Content="2" HorizontalAlignment="Left" Margin="90,0,0,0" VerticalAlignment="Top" Width="75" CommandParameter="2" Command="{Binding NumClickedCommand}"/> 
     <Button Content="3" HorizontalAlignment="Left" Margin="170,0,0,0" VerticalAlignment="Top" Width="75" CommandParameter="3" Command="{Binding NumClickedCommand}"/> 
     <Button Content="4" HorizontalAlignment="Left" Margin="10,27,0,0" VerticalAlignment="Top" Width="75" CommandParameter="4" Command="{Binding NumClickedCommand}"/> 
     <Button Content="5" HorizontalAlignment="Left" Margin="90,27,0,0" VerticalAlignment="Top" Width="75" CommandParameter="5" Command="{Binding NumClickedCommand}"/> 
     <Button Content="6" HorizontalAlignment="Left" Margin="170,27,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="1.034,2.171" CommandParameter="6" Command="{Binding NumClickedCommand}"/> 
     <Button Content="7" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" Width="75" CommandParameter="7" Command="{Binding NumClickedCommand}"/> 
     <Button Content="8" HorizontalAlignment="Left" Margin="90,54,0,0" VerticalAlignment="Top" Width="75" CommandParameter="8" Command="{Binding NumClickedCommand}"/> 
     <Button Content="9" HorizontalAlignment="Left" Margin="170,54,0,0" VerticalAlignment="Top" Width="75" CommandParameter="9" Command="{Binding NumClickedCommand}"/> 
     <Button Content="0" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top" Width="75" CommandParameter="0" Command="{Binding NumClickedCommand}"/> 
     <Button Content="Submit" HorizontalAlignment="Left" Margin="90,81,0,0" VerticalAlignment="Top" Width="155" /> 

    </Grid> 
</UserControl> 

問題はのviewmodel doesntの中のProductIdをupdaditingするビューに反映していることです。 asdの初期値は、アプリケーションの起動時に更新されます。コントロールには、ICommandインターフェイスを実装する一連のボタンが含まれており、すべてviewmodelでNumClicked()を呼び出します。デバッグ中に、NumClicked()ボタンがクリックされ、ProductIdが更新され、NotifyPropertyChanged()も呼び出されますが、UIは更新されません。しかし、私はUIを直接更新する場合、つまり、同じフローが発生した場合、PropertyChanged()が呼び出され、その後getが呼び出され、viewmodelの値が更新されます。

私は既にこのような多くの疑問を抱いていますが、正確にUIの更新を停止することはできません。どんな援助も感謝し、欠けているものがあれば尋ねる。 ありがとうございます。

+1

と 'NumClickedCommand'が使用されているのDataContextにアクセスすることができますか? – ASh

+0

あなたは問題がNumClickedCommandで、ProductIdのupdaditingではないと思います – Amine

+0

ProductIdを更新していないのですか? ProductIdは、ボタンをクリックするたびに更新されています – AnoopDV

答えて

1

DataContextGridおよびTextBoxは、ウィンドウリソースからのビューモデルインスタンスにバインドされています。

NumPad制御が

NumPadDataContextを継承しますが、ビューモデルのインスタンスを1つだけ持っていることを確認し

NumClickedCommand間違ったデータを持つ作品ではなく、表示されたオブジェクトとビューモデルの独自のインスタンスを宣言し、新しいオブジェクトを作成して変更しないでください。DataContext

<UserControl x:Class="Billing.NumPad" 
     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:viewModel="clr-namespace:Market.ViewModel;assembly=Market.ViewModel" 
     mc:Ignorable="d" Height="109" Width="248"> 

<Grid Height="109" VerticalAlignment="Top"> 
    <!--all Buttons here-->  
</Grid> 
0

同じviewmodelをnumpadUser controllとmainWindowにDataContextとして渡しているので、viewmodelの新しいインスタンスが作成されるので、NumwindのGridNameを使ってNumPadでmainwindowのDataContextを使用することができます。

<Grid DataContext="{Binding Source={StaticResource ViewModel}}" x:Name="grdNumPad"> 

、あなたのNumPad.XAMLにこのよう

<Button Content="1" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="75" CommandParameter="1" Command="{Binding DataContext.NumClickedCommand,ElementName=grdNumPad}"/> 
関連する問題