2012-02-16 7 views
2

私が抱えている問題に対処する最善の方法については疑問に思っています。 私は初期段階で、個々のクラスがすべて であるいくつかの画像を持つWPFアプリケーションを開発しています。私はMVVMモデルを使用しようとしていますが、画像上をスクロールしていくつかの情報を表示したり、画像上にいくつかの効果を加えたい場合にマウスオーバーイベントを実装したいと考えています。私は下のクラスの例を持っています。MVVMの個々のオブジェクトのマウスオーバーイベント

public class TeamKit 
{ 
    private Image mainImage; 
    public Canvas Position { get; set; } 
    public TextBlock PlayerText { get; set; } 
    public TextBlock NameText{ get; set; } 
    public Player Player { get; set; } 
    private string _playerName = "Player0"; 
    private string _playerNumber = "0"; 
    private BitmapImage _bImage; 

    public TeamKit(Thickness t) 
    { 
     mainImage = new Image(); 
     _bImage = new BitmapImage(new Uri("pack://Application:,,,/Resources/KitC.png")); 
     mainImage.Source = _bImage; 
     mainImage.Stretch = System.Windows.Media.Stretch.None; 
     Position = new Canvas(); 
     Position.Width = 38; 
     Position.Height = 45; 
     Position.Margin = t; 
     mainImage.Margin = new Thickness(0, 0, 0, 6); 

     PlayerText = new TextBlock(); 
     PlayerText.Text = ""; PlayerText.TextAlignment = TextAlignment.Center; 
     PlayerText.Margin = new Thickness(11, 15, 27, 15); 

     Position.Children.Add(mainImage); 
     Position.Children.Add(PlayerText); 
    } 

    public TeamKit() 
    { 
     mainImage = new Image(); 
     _bImage = new BitmapImage(new Uri("pack://Application:,,,/Resources/KitC.png")); 
     mainImage.Source = _bImage; 
     mainImage.Stretch = System.Windows.Media.Stretch.None; 
     Position = new Canvas(); 
     Position.Width = 38; 
     Position.Height = 45; 
     //mainImage.Margin = new Thickness(0, 0, 0, 6); 

     PlayerText = new TextBlock(); 
     PlayerText.Text = _playerNumber; PlayerText.TextAlignment = TextAlignment.Center; 
     PlayerText.Margin = new Thickness(12, 15, 27, 15); 
     PlayerText.Width = 15; 

     Viewbox Vb = new Viewbox(); 
     //Vb.StretchDirection = StretchDirection.Both; 
     Vb.Stretch = System.Windows.Media.Stretch.Uniform; 
     Vb.VerticalAlignment = VerticalAlignment.Stretch; 
     Canvas.SetTop(Vb, 40); 
     Canvas.SetLeft(Vb, -11); 
     Vb.MaxWidth = 50; 

     NameText = new TextBlock(); 
     NameText.Text = _playerName; 
     NameText.TextAlignment = TextAlignment.Center; 
     NameText.MaxHeight = 40; 
     NameText.MaxWidth = 90; 

     Vb.Child = NameText; 
     Position.Children.Add(Vb); 
     //<TextBlock Text="FooAlanghi" TextWrapping="Wrap" TextAlignment="Center" MaxHeight="40" MaxWidth="92" /> 


     Position.Children.Add(mainImage); 
     Position.Children.Add(PlayerText); 
    } 

    public TeamKit(Player Player, int PlayerNumber) 
    { 
     this.Player = Player; 
     _playerNumber = PlayerNumber.ToString(); 
     _playerName = Player.Last_Name; 

     mainImage = new Image(); 
     _bImage = new BitmapImage(new Uri("pack://Application:,,,/Resources/KitC.png")); 
     mainImage.Source = _bImage; 
     mainImage.Stretch = System.Windows.Media.Stretch.None; 
     Position = new Canvas(); 
     Position.Width = 38; 
     Position.Height = 45; 
     //mainImage.Margin = new Thickness(0, 0, 0, 6); 

     PlayerText = new TextBlock(); 
     PlayerText.Text = _playerNumber; PlayerText.TextAlignment = TextAlignment.Center; 
     PlayerText.Margin = new Thickness(12, 15, 27, 15); 
     PlayerText.Width = 15; 

     Viewbox Vb = new Viewbox(); 
     //Vb.StretchDirection = StretchDirection.Both; 
     Vb.Stretch = System.Windows.Media.Stretch.Uniform; 
     Vb.VerticalAlignment = VerticalAlignment.Stretch; 
     Canvas.SetTop(Vb, 40); 
     Canvas.SetLeft(Vb, -11); 
     Vb.MaxWidth = 50; 

     NameText = new TextBlock(); 
     NameText.Text = _playerName; 
     NameText.TextAlignment = TextAlignment.Center; 
     NameText.MaxHeight = 40; 
     NameText.MaxWidth = 90; 

     Vb.Child = NameText; 
     Position.Children.Add(Vb); 
     //<TextBlock Text="FooAlanghi" TextWrapping="Wrap" TextAlignment="Center" MaxHeight="40" MaxWidth="92" /> 


     Position.Children.Add(mainImage); 
     Position.Children.Add(PlayerText); 
    } 


    public void Add(Panel Parent) 
    { 
     Parent.Children.Add(this.Position); 
    } 

    public static void DrawPositionLineUp(List<TeamKit> Players, Panel panel, double top, double left) 
    { 
     double ix = 0; 
     foreach (TeamKit t in Players) 
     { 
      Canvas.SetLeft(t.Position, left); 
      Canvas.SetTop(t.Position, ix += top); 
      t.Add(panel); 
     } 
    } 
} 

答えて

3

あなたはMVVMルートを使用していることがうれしいですが、あなたがやりたいことはMVVMとは関係ないことに注意してください。あなたはすべてのビュー関連(よく、ほとんど)の質問です。 、それらの多くは、すでに(ぼかし、影など)に組み込まれている効果に応じて、効果については

<Image ...> 
    <Image.ToolTip> 
     <ToolTip ...> 
      Content (which can be another layout of controls) 
     </ToolTip> 
    </Image.ToolTip> 
</Image> 

あなたしているコントロールがToolTipを必要とします。しかし、どちらの方法でも、スタイル内でトリガーを使いたいと思っています。

<Image ...> 
    <Image.Style> 
     <Style> 
      <Trigger Property="Image.IsMouseOver" Value="True"> 
       <Setter ... /> <!-- Apply Styles Here --> 
      </Trigger> 
     </Style> 
    </Image.Style> 
</Image> 

注:あなたがそのように、それは、チェーン(アプリケーション)に/そのウィンドウ内のユーザーコントロール/ページまたはアップ高を静的リソースにスタイルを引き出し、その種類のすべてのコントロールに適用するために、おそらく最善です

<Image Style="{StaticResource MouseOverImage}" ... /> 

は、私が言った理由については...行うことができます「あなたが質問していることで、すべてのビューに関連した(まあ、ほとんどが)」それはビュー関連のアップであることを...私が何を意味するかですデータバインディングのポイントまで、必要なプロパティが公開されていることを確認するためにビューモデルと調整する必要があります。それ以外は、ビュー関連の100%の質問です。この状況でMVVMnessについて心配する必要はありません。 MVVMの使用を続けますが、MVVMを使用しているかどうかに関わらず、これを実行する方法がわかります。

2

トリガーの使用を検討しましたか? マウスオーバーイベントを処理するときは、通常はトリガーを使用しますが、MVVM light(または類似のもの)をMVVM愛好者のためのすばらしいツールと考えるべきです。

+0

トリガーは確かに調べるべきものですが、残念なことに環境上の制約のために私はオンボードではないものはインストールできません – Infolord101

+1

@LukeShoge [Triggers](http://msdn.microsoft.com/en-us/library/ system.windows.trigger.aspx)は、WPFの組み込みの一部です – Rachel

関連する問題