2016-10-31 96 views
0

我试图绑定一个变量来显示标签的内容。 我使用Galasoft's MVVM light来实现这一点。 我发送我的viewmodel,我的window和我的window.cs。问题是,当我增加值时,什么都不会发生,但是应该做些什么。MVVM light Binding Path not working

窗口:我已经结合上只有一个标签

<Window x:Class="DPCKOU_prog3hf_pong.Settings" 
    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:DPCKOU_prog3hf_pong" 
    mc:Ignorable="d" 
    Title="Settings" Height="406" Width="717" 
    Background="{StaticResource MainMenuBG}" 
    ResizeMode="NoResize" 
    WindowStartupLocation="CenterScreen" 
    > 
<Grid> 
    <StackPanel> 
     <Label x:Name="Title" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch" 
       Foreground="#00e4ff" FontSize ="28" Content="Size of pad" 
       Margin="10,30,10,10"> 
      <Label.BitmapEffect> 
       <DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/> 
      </Label.BitmapEffect> 
     </Label> 
     <Label x:Name="Size" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch" 
       Foreground="#00e4ff" FontSize ="40" Content="{Binding Path=PadSize}" 
       Margin="10,0,10,10"> 
      <Label.BitmapEffect> 
       <DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/> 
      </Label.BitmapEffect> 
     </Label> 
     <DockPanel> 
      <Button x:Name="Increase" Content="Increase" HorizontalAlignment="Left" 
      Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="120,10,10,10" 
      FontSize="18" BorderThickness="0" Click="Increase_Click"> 
       <Button.BitmapEffect> 
        <DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/> 
       </Button.BitmapEffect> 
      </Button> 
      <Button x:Name="Decrease" Content="Decrease" HorizontalAlignment="Right" 
      Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,120,10" 
      FontSize="18" BorderThickness="0" Click="Decrease_Click"> 
       <Button.BitmapEffect> 
        <DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/> 
       </Button.BitmapEffect> 
      </Button> 
     </DockPanel> 
     <Button x:Name="Play" Content="Play" HorizontalAlignment="Center" 
      Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,10,10" 
      FontSize="18" BorderThickness="0" Click="Play_Click"> 
      <Button.BitmapEffect> 
       <DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/> 
      </Button.BitmapEffect> 
     </Button> 
     <Button x:Name="Back" Content="Back" HorizontalAlignment="Center" 
      Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,10,10" 
      FontSize="18" BorderThickness="0" Click="Back_Click"> 
      <Button.BitmapEffect> 
       <DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/> 
      </Button.BitmapEffect> 
     </Button> 
    </StackPanel> 
</Grid> 

该模型的cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using GalaSoft.MvvmLight; 

namespace DPCKOU_prog3hf_pong 
{ 
    class SettingsVM : ViewModelBase 
    { 
     int padSize; 
     public int PadSize 
     { 
      get 
      { 
       return padSize; 
      } 
      set 
      { 
       if(value >=1 && value <= 6) 
       { 
        Set(ref padSize, value); 
       } 
      } 
     } 
     public SettingsVM() 
     { 
      padSize = 1; 

     } 
    } 
} 

window.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.Shapes; 
using GalaSoft.MvvmLight; 

namespace DPCKOU_prog3hf_pong 
{ 
    /// <summary> 
    /// Interaction logic for SingleSettings.xaml 
    /// </summary> 
    ///   
    /* 
    * take it 100 pixels is the 4 units of the pad's size respectively. 
    * so 25 shall be 1 and that is the default value one can input. 
    * the player can input pad size from 1 to 6 meaning from 25 pixels  to 150. 
*/ 

public partial class Settings : Window 
{ 
    /* 
    * its sole purpose is to change the pad size. 
    */ 
    SettingsVM svm; 
    bool isPlaymodeSingle; 
    public Settings(bool isPlaymodeSingle) 
    { 
     InitializeComponent(); 
     this.isPlaymodeSingle = isPlaymodeSingle; 
     svm = new SettingsVM(); 
    } 

    private void Play_Click(object sender, RoutedEventArgs e) 
    { 
     if (isPlaymodeSingle) 
     { 
      //yes, solo 
      SinglePlayer spgame = new SinglePlayer(); 
      spgame.Show(); 
      Close(); 
      /* 
      * show the game window and close this. 
      */ 
     } 
     else 
     { 
      //nope, multi. 

     } 
    } 

    private void Decrease_Click(object sender, RoutedEventArgs e) 
    { 
     svm.PadSize--; 
    } 

    private void Increase_Click(object sender, RoutedEventArgs e) 
    { 
     svm.PadSize++; 
    } 

    private void Back_Click(object sender, RoutedEventArgs e) 
    { 
     MainWindow mw = new MainWindow(); 
     mw.Show(); 
     Close(); 
    } 


} 
} 
+3

显然你从来没有设置窗口的DataContext的。将'DataContext = svm;'添加到Settings构造函数中。 – Clemens

+0

你不是一天中的英雄吗?D它诀窍。您可能想将其转换为答案,以便我可以接受。 – agiro

回答

1

你忘了设置窗口的DataContext

你可以做,例如在设置类的构造函数:

public Settings(bool isPlaymodeSingle) 
{ 
    InitializeComponent(); 
    this.isPlaymodeSingle = isPlaymodeSingle; 
    svm = new SettingsVM(); 

    DataContext = svm; // here 
}