MVVM光绑定路径无法正常工作

时间:2016-10-31 14:51:14

标签: c# wpf mvvm

我试图绑定一个变量以显示在标签的内容中。 我使用Galasoft's MVVM light来实现这一目标。 我发送了viewmodel,我的windowwindow.cs。问题是当我增加值时没有任何事情发生,但事情应该发生。

窗口:我只绑定了一个标签

<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();
    }


}
}

1 个答案:

答案 0 :(得分:1)

您忘了设置窗口的DataContext

例如,您可以在Settings类的构造函数中执行此操作:

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

    DataContext = svm; // here
}