2016-03-24 13 views
1

My class "ship"はプロパティ "ShipOnePos"の値を連続的に変更する必要があります。この値は、UI要素(画像)プロパティのいずれかにバインドされます。私は "ShipOnePos"(変更されたとき)に従ってUIを更新したい。このため、私はインターフェイスINotifyPropertyChangedを使用しますが、UIは更新されず、PropertyChanged値は常にnullです。INotifyPropertyChanged - 常にnull

私が逃したもの、またはこの実装で何が間違っているかアドバイスできますか?

クラス:

namespace DzienNaWyscigach 
 
{ 
 
    public class ship : INotifyPropertyChanged 
 
    { 
 
     
 
     public event PropertyChangedEventHandler PropertyChanged; 
 
     private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
 
     { 
 
      if (PropertyChanged != null) 
 
      { 
 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
 
      } 
 

 
     } 
 

 
     private int MyShipOnePos; 
 

 
     public int ShipOnePos 
 
     { 
 
      get { return MyShipOnePos; } 
 
      set { MyShipOnePos = value; 
 
        NotifyPropertyChanged(); 
 
       } 
 
     } 
 

 
     public void ShipsMovment() 
 
     { 
 
      DispatcherTimer timer = new DispatcherTimer(); 
 
      timer.Interval = TimeSpan.FromMilliseconds(500); 
 
      timer.Tick += Timer_Tick; 
 
      timer.Start(); 
 
      
 
     } 
 

 
     private void Timer_Tick(object sender, EventArgs e) 
 
     { 
 
      ShipOnePos= ShipOnePos+10; 
 

 
     } 
 
    } 
 
}

<Image x:Name="ShipOne" HorizontalAlignment="Left" Height="71" Margin="31,32,0,0" VerticalAlignment="Top" Width="80" Source="Asets/ship1.png" RenderTransformOrigin="0.5,0.5"> 
 
      <Image.DataContext> 
 
       <local:ship/> 
 
      </Image.DataContext> 
 
      <Image.RenderTransform> 
 
       <TransformGroup> 
 
        <ScaleTransform/> 
 
        <SkewTransform/> 
 
        <RotateTransform/> 
 
        <TranslateTransform X="{Binding Path=ShipOnePos, Mode=OneWay}"/> 
 
       </TransformGroup> 
 
      </Image.RenderTransform>

コードをバインドUI

​​ 背後

+0

あなたは彼がCallerMemberName属性 –

+1

@GordonAllocmanにあなたがそれを呼び出すあなたのNotifyPropertyChangedメソッドにプロパティ名を渡していません、私は逃しました。 – WasGoodDone

+0

@WasGoodDone良いキャッチを使用していますので、必要はありませんあなたのセッター –

答えて

1

shipあなたのクリックで作成しているが、あなたがあなたのImageを拘束shipではありません。おそらくshipをリソースとして作成し、Clickイベントでそれを取得して既存のshipを取得し、移動を開始する必要があります。または、shipプロパティを持つ親VMを持つことができます。

したがって、たとえば、あなたはこのような何か行うことができます:あなたのクリックハンドラで

<Window.Resources> 
    <local:ship x:Key="myShip"/> 
</Window.Resources> 
.... 
<Image x:Name="ShipOne" 
    HorizontalAlignment="Left" Height="71" 
    Margin="31,32,0,0" VerticalAlignment="Top" Width="80" Source="Asets/ship1.png" 
    RenderTransformOrigin="0.5,0.5" 
    DataContext="{StaticResource myShip}"> 
... 
</Image> 

そして:

private void BTN_Start_Click(object sender, RoutedEventArgs e) 
{ 
    // Note: it might be worth caching this in a field or property 
    ship Ships = FindResource("myShip") as ship; 
    Ships.ShipsMovment(); 
} 

または代わりに、親VMのアイデアを使用して:

public class ParentVM : INotifyPropertyChanged 
{ 
    private ship _currentShip; 
    public ship CurrentShip 
    { 
     get { return _currentShip; } 
     set { _currentShip = value; NotifyPropertyChanged(); } 
    } 
    // .... 
} 

それから、あなたはできる:

<Window.Resources> 
    <local:ParentVM x:Key="myVM"/> 
</Window.Resources> 
... 
<Grid DataContext="{StaticResource myVM}"> 
    .... 
    <Image DataContext="CurrentShip" ...> 
     .... 
    </Image> 
</Grid> 

そして:

// Note: this might be better handled with a command rather than a click handler 
private void BTN_Start_Click(object sender, RoutedEventArgs e) 
{ 
    // Note: it might be worth caching this in a field or property 
    ParentVM vm = FindResource("myVM") as ParentVM; 
    vm.CurrentShip = new ship(); 
    vm.CurrentShip.ShipsMovment(); 
} 
+0

マットありがとう、今働いています – Piotr