2016-04-29 15 views
0

夕方すべて、クラスのメンバー変数からのデータバインディング。 WPF

データバインディングを使用してクラスの変数をUIにバインドするWPFアプリケーションで作業しています。他のプロジェクトでうまく動作しているINotifyPropertyChangedを組み込むようにクラスを設定しました。これは、現在、次のようになります。

namespace playerWPF 
{ 
    class GameInfo : INotifyPropertyChanged 
    { 

     public GameInfo(string _teamName) //constructor 
     { 
      this.TeamName = _teamName; 
      this.OppositionName = "Opposition"; 
      this.TeamScore = 0; 
      this.OppositionScore = 0; 
      this.Question = "What is your team name?"; 



     } 

     public event PropertyChangedEventHandler PropertyChanged; 

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

     private string question; 

     public string Question 
     { 
      get 
      { 
       return question; 
      } 
      set 
      { 
       question = value; 
       OnPropertyChanged("Question"); 
      } 
     } 

//and so on for all the other variables in a similiar fashion 

私のXAMLは次のようになります。private static GameInfo gameInfo;

とIn:

<Window x:Class="playerWPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:playerWPF" 
     xmlns:system="clr-namespace:System;assembly=mscorlib" 
     Title="MainWindow" Height="350" Width="525" 
     Name="MainWindowName"> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="112*"/> 
      <RowDefinition Height="207*"/> 
     </Grid.RowDefinitions> 
     <TextBox x:Name="answerBox" HorizontalAlignment="Left" Height="32" Margin="58,60,0,0" TextWrapping="Wrap" Text="Answer here..." VerticalAlignment="Top" Width="371" Grid.Row="1"/> 
     <Button x:Name="submitButton" Content="Submit" Click="submitButton_Click" HorizontalAlignment="Left" Height="35" Margin="199,97,0,0" VerticalAlignment="Top" Width="92" Grid.Row="1"/> 
     <Label x:Name="teamNameLabel" Content="{Binding TeamName}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="25" Width="74"/> 
     <Label x:Name="oppositionName" Content="{Binding OppositionName}" HorizontalAlignment="Left" Margin="328,9,0,0" VerticalAlignment="Top" Width="101"/> 
     <Label x:Name="responseLabel" Content="{Binding Question}" HorizontalAlignment="Left" Height="49" Margin="58,6,0,0" VerticalAlignment="Top" Width="371" Grid.Row="1"/> 
     <Label x:Name="teamScoreLabel" Content="{Binding TeamScore}" HorizontalAlignment="Left" Margin="19,35,0,0" VerticalAlignment="Top" Width="65"/> 
     <Label x:Name="oppositionScoreLabel" Content="{Binding OppositionScore}" HorizontalAlignment="Left" Margin="340,40,0,0" VerticalAlignment="Top" Width="65"/> 

    </Grid> 
</Window> 

私はこのようにように私のMainWindowの静的フィールドとして、このクラスの型を初期化しますメインウィンドウのコンストラクタ私はそれに新しいインスタンスを割り当てます:gameInfo = new GameInfo("Team");

これはいくつかのUIのラベルをデフォルトで構築された値に変更しますが、そうではありません。誰でも光を当てることはできますか?

+0

また単に私がのDataContextを持っていることを言及する価値がある

DataContext = gameInfo; 

DataContext = this; //you don't want the data context to be the window itself 

を変更する=この;私のMainWindowコンストラクタでも同様です。 – James

+0

DataContextは 'gameInfo'で、バインディングは' gameInfo.'を含んではいけません。単にプロパティ名です。 – Crowcoder

+0

@Crowcoder私は自分のバインディングを変更し、私の質問を編集しましたが(特定のクラスインスタンスの値をどのように反映するのでしょうか?)、私は、DataContextが私が持っているMainWindowのコンストラクタ既に。私は他の場所に置かれたことは一度も見たことがありません – James

答えて

2

MVVMパターンに従っている場合は、View ModelのようにgameInfoを利用します。だから、その後、あなたのバインディングが

{Binding <property name>} 
関連する問題